0

I have tried my best. but it is still giving me blank page. My goal is to Find and Replace words in post content article for one or two specific category, Not the entire site.

function specific_category_postsfilter($content) {

    if (is_single() && in_category('Uncategorized')) {
        $old_word = 'Listen Below';
    $new_word = 'Just Now';
    str_replace($old_word, $new_word, $content);
    }
}
     add_filter('the_content', 'specific_category_postsfilter');

1 Answers1

0

You're actually pretty close! If you look at the docs for the_content filter, (actually any filter, for that matter) you'll see that it requires the filtered values to be returned.

Also if you look at the docs for str_replace() you'll see that you need to do something with the returned value.

So, with those in mind we can change it to str_replace the words in $content only if it's a single post in 'Uncategorized', and then always return the $content afterwards, modified or unmodified:

add_filter( 'the_content', 'replace_content_in_category' );
function replace_content_in_category( $content ){
    if( is_single() && in_category( 'Uncategorized' ) ){
        $old_word = 'Listen Below';
        $new_word = 'Just Now';

        $content = str_replace( $old_word, $new_word, $content );
    }

    return $content;
}
Xhynk
  • 13,513
  • 8
  • 32
  • 69