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


  • 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;


Try this code.

add_filter('wp_title', 'custom_title');
function custom_title($title) {
    if(is_single()) {
        $category = get_the_category(get_the_ID());
            $categories = array('music','video');
                return $category[0]->name.': '.$title;
    return $title;
  • 1
    Thanks @tanmaypatel Feb 24 '20 at 22:06

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.