0

I'm using the 2016 template and it uses wp_head() to render the title tag. So for Category pages it displays

 Articles about: whatever

How do I modify this?

EDIT: I ended up doing this hack. Which I hate...

remove_action( 'wp_head', '_wp_render_title_tag', 1 );
add_action( 'wp_head', '_wp_render_title_tag_jch', 1 );

function _wp_render_title_tag_jch() {
    if ( did_action( 'wp_head' ) || doing_action( 'wp_head' ) ) {     
       $t = wp_get_document_title();
       if( strpos($t, 'Weekly Updates',0) >= 0 )
          echo( '<title>' . 'Weekly Updates - Example Site' . '</title>' . "\n");
        else
          echo( '<title>' . $t . '</title>' . "\n");              
    }
}
0

The default WordPress themes such as Twenty Sixteen and Twenty Twenty-One are using the automatic <title> tag feature (or the title-tag theme support) introduced in WordPress 4.1, so with that feature, if you want to modify the title, you would want to use the document_title_parts hook:

add_filter( 'document_title_parts', 'my_document_title_parts' );
function my_document_title_parts( $title ) { // $title is an *array*
    if ( is_category() ) {
        $title['title'] = 'Posts in ' . single_cat_title( '', false );
    }

    return $title;
}

UPDATE

Actually, looking at the code (or ugly hack) in the edited question, I think my sample code above did not work because the is_category() returned a false and it's probably because the "Weekly Updates" is a Page (post of the page type) and not actually a default category archive at /category/<category slug>, e.g. example.com/category/uncategorized.

So let me put it this way: For themes like Twenty Sixteen which support the title-tag, you should use the document_title_parts hook to filter the title tag value, i.e. <title>THIS PART</title>. But as for the actual code which modifies the title, it will be up to you on how should you write your code.

Nonetheless, try this (and be sure to remove the ugly hack in your code):

add_filter( 'document_title_parts', 'my_document_title_parts' );
function my_document_title_parts( $title ) {
    if ( false !== strpos( $title['title'], 'Weekly Updates' ) ) {
        $title['title'] = 'Weekly Updates';
    }

    return $title;
}
/* NOTE: Try using a greater number as the callback priority, if the default (10)
 * doesn't work - example with 9999 as the priority:
add_filter( 'document_title_parts', 'my_document_title_parts', 9999 );
*/

And note that the hook has one parameter which is $title and it's an array that consists of (but not all items present on all pages):

  • 'title' (string) Title of the viewed page.
  • 'page' (string) Optional. Page number if paginated.
  • 'tagline' (string) Optional. Site description when on home page.
  • 'site' (string) Optional. Site title when not on home page.

Also, if you want to change the title separator (like -), then you can use the document_title_separator filter.

8
  • That doesn't work. I'm using Yoast. I have dug into the plug and can't see where to edit that Title tag. – jchwebdev Feb 23 at 20:37
  • Try clearing your site and browser caches, and see if the title changes. If not, then try deactivating Yoast (and/or other plugins) and see if the above filter works, because it worked for me on a default setup of WordPress with the Twenty Sixteen theme. But were you actually referring to category URLs like example.com/category/some-category and that by "2016 template", you indeed meant the Twenty Sixteen theme? – Sally CJ Feb 24 at 0:42
  • Or did you possibly mean a custom Page template - if so, try checking the SEO settings on the edit page screen? And if I were you, I'd try doing a grep/search in the themes and plugins directories for files containing "articles about". – Sally CJ Feb 24 at 0:45
  • Its gptta be some nonsense in Yoast, but I did a search of the code and can't find any direct matches. I ended up doing a hack which I put in the original question. It does not thrill me, but I don't know what else to do at this point. – jchwebdev Feb 24 at 22:31
  • Well, that's a seriously ugly hack. Have you tried deactivating Yoast and did my code then work? Are you sure it's a Yoast issue? If so, you should contact the author and report the issue. And you could actually try again with my code, but use a higher number as the priority: add_filter( 'document_title_parts', 'my_document_title_parts', 9999 ); – Sally CJ Feb 24 at 23:16

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.