0

Prelude:

Let's say the text "Comments are closed" is a default WP String and you wanna change that text string to something else like "Hello World!" if is_singular('post-type') is true.

Issue:

Now, I do know that there is a plugin called Say What? which does this exact thing, you just have to provide the string of text and the respective text domain. But the problem is that whatever you change in this plugin it will become global for the entire website. You cannot use and conditional check and then change the text if the condition is true.

So, can any of you let me know if I can change the text without this plugin via normal code so that I can use any conditional statement I want.

Any help will be appreciated. Thanks.

  • Generally the "comments are closed" text is added via the theme, normally in the comments.php file, so you would do the check in there or replace it with a function and do the checks in the function in the functions.php – Gareth Gillman Oct 21 '15 at 10:56
1

Yes you can do it, just find the file where the text resides, maybe single.php or single-{post_type}.php or archive-{post_type}.php Then, look for the string you want to change erase it and set a variable, something like this:

If the file where the string is, is a template file for that specific post type, the you will only replace the string for whatever you want:

<h2>Comments are closed<h2> //Replace comments are closed for Hello World

Remember the strings you find in comments, footer or sidebar are in separate files, and, in that case, and in the case the file that is giving the HTML structure to the post type is single.php, you'll have to do something like this inside the loop:

<?php
$post_type = get_post_type( get_the_ID() ); 
$the_string = "comments are closed";
if ($post_type === "my_post_type") {
$the_string = "Hello World!";
}
?>

<h2><?php echo $the_string; ?></h2>
  • Thanks a lot. Is there no other way to do that? Like using the text domain? – iSaumya Oct 21 '15 at 11:32
  • Maybe using some hook for some parts. You can do it with JavaScript/jQuery, and Maybe with plugins like translate Loco or editing the .mo .po files if the post type has a custom template. You can even change it with css using :before or :after and applying display: none; to the tag that wraps the string, but again, will only work if the post type has a custom template. I think the bes option is to do it with php. – Eduardo Sánchez Hidalgo Urías Oct 21 '15 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.