0

This code snippet shows (prefix) category Name first before the Post title (Very right).

E.g

  • Music: %Post_title%
  • Video: %post_title%
  • Lyrics: %post_title%
  • News: %post_title%
  • Lifstyle: %Post_title%.

Now the problem is, I wanted to show this on a very specific category(Music & Video) alone and not all categories...

E.g on category Music & Video alone.

Then any other post from any other category will not show any Prefix at all...

Below is the code snippet:

add_filter('wp_title', function ($title) {


if (is_single()) {
    $categories = get_the_category(get_the_ID());
    // Assuming the post has many categories will take the first
    $category = reset($categories);
    return $category->name .': '.$title; 
}
return $title;
}


);
1

Try this code.

add_filter('wp_title', 'custom_title');
function custom_title($title) {
    if(is_single()) {
        $category = get_the_category(get_the_ID());
        if(!empty($category)){
            $categories = array('music','video');
            if(in_array($category[0]->slug,$categories)){
                return $category[0]->name.': '.$title;
            }
        }
    }
    return $title;
}
| improve this answer | |

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.