Celebrating 7 Years! Get up to $30 off GeneratePress Premium!

[Resolved] Add conditional label to category and tag archive page titles

Home Forums Support [Resolved] Add conditional label to category and tag archive page titles

Home Forums Support Add conditional label to category and tag archive page titles

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #488026
    Denise

    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

    GeneratePress 2.0.2
    GP Premium 1.5.6
    #488140
    Tom
    Lead Developer
    Lead Developer

    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 πŸ™‚

    #488975
    Denise

    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

    #488978
    Denise

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

    Thanks,
    Denise

    #489077
    Tom
    Lead Developer
    Lead Developer

    Did you add it to a child theme? I just made an adjustment above – let me know if it works now πŸ™‚

    #489081
    Denise

    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

    #489083
    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

    #489113
    Tom
    Lead Developer
    Lead Developer

    Glad it’s working now πŸ™‚

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.