Add string to page title if post in specific category

If you meant the title in the title tag as in <title>here</title> on single post pages, then you might want to use the single_post_title filter to prepend the string in question to the page title. E.g.

add_filter( 'single_post_title', 'recipe_single_post_title', 10, 2 );
function recipe_single_post_title( $title, $post ) {
    if ( in_category( 'recipes', $post ) ) {
        return "Recept: $title";
    }

    return $title;
}

But there are other filters you can try:

  • If your theme supports the automatic <title> tag (and BTW it actually should support it), e.g. in the functions.php file it calls add_theme_support( 'title-tag' ), then you can use the document_title_parts or document_title filter. E.g. Using the former:

    add_filter( 'document_title_parts', 'recipe_page_title_parts' );
    function recipe_page_title_parts( $title ) {
        if ( is_single() && in_category( 'recipes' ) ) {
            $title['title'] = 'Recept: ' . $title['title'];
        }
    
        return $title;
    }
    
  • If (for whatever reasons) your theme is hard-coding the <title> tag into the header template (header.php), then you can use the wp_title_parts or wp_title filter. E.g. Using the former:

    add_filter( 'wp_title_parts', 'recipe_page_title_parts2' );
    function recipe_page_title_parts2( $title ) {
        if ( is_single() && in_category( 'recipes' ) ) {
            $title[0] = 'Recept: ' . $title[0];
        }
    
        return $title;
    }
    

tech