Site logo

Archived topic

Add conditional label to category and tag archive page titles

7 replies · Started by Denise on February 4, 2018

Viewing posts 1–8 of 8

Hello GP Friends,

I would like to modify the page title for all category and tag archives.
-- for categories, append the words 'Resource Type: ' before the title
- for tags, append the word 'Topic: ' before the title
For example:
If the archive is a category called Videos, I would like the page title to be: 'Resource Type: Videos'
If the archive is a tag called Healthcare, I would like the page title to be 'Topic: Healthcare'

Can I do this with a hook and filter using generate_archive_title?
I'm using a child theme, with GP Pro.

Thanks,
Denise

Hi there,

You should be able to use this filter: get_the_archive_title

For example:

add_filter( 'get_the_archive_title', 'tu_adjust_archive_titles', 20 );
function tu_adjust_archive_titles( $title ) {
    if ( is_tag() ) {
        return 'Topic: ' . single_tag_title( '', false );
    }

    if ( is_category() ) {
        return 'Resource Type: ' . single_cat_title( '', false );
    }

    return $title;
}

Hope this helps :)

Hi Tom,
Thanks for the code. That's the same as what I was trying to use, but I can't get it to work.
I tested it on my staging site on a bare bones install with GP Pro, a child theme, no pluginis, nothing in the css, and only your code in the functions.php. It didn't change the archive title. The regular archive heading with just the tag (or category) word was displayed, as it usually is.

However, if I put similar code in the GP Hooks, after the heading, it does show up, so something is working:
<?php
if ( is_tag() ) {
echo 'Topic: ' . single_tag_title( '', false );
}

if ( is_category() ) {
echo 'Resource Type: ' . single_cat_title( '', false );
}
?>

I wonder if it has to do with settings in the customizer? I played around with changing them, but it didn't change anything. Then I tried playing with the permalinks, but that had no affect.

Thanks for your help,
Denise

Is it possible that I need to remove a filter before applying the new one?

Thanks,
Denise

Did you add it to a child theme? I just made an adjustment above - let me know if it works now :)

Hi Tom,

I see you added the second parameter (20).
I tried it and still no luck.
I am using a child theme. And I do not have any code in the hooks. It's all in functions.php. But, as I mentioned, the only code I have in that file is this snippet from you.

I'm baffled. This seems like it should work. I'll try some more things in the customization settings. Or maybe it's a caching thing...? I tried running it on different browsers, and with incognito, and clearing the cache. Still no luck.

By the way, I'm a database programmer and have been writing code for over fifteen years, so I'm used to weird things that aren't quite what we would call "expected behavior" =)

Best wishes,
Denise

Ok, I got it working. I'm not sure what fixed it, though, which is always a strange thing. My laptop had a little melt down last week and hasn't been the same since...

You can close this ticket.

Thanks,
Denise

Glad it's working now :)

This archived topic is closed to new replies.