[Resolved] How to add a suffix to the Category (Archive) Title?

Home Forums Support [Resolved] How to add a suffix to the Category (Archive) Title?

Home Forums Support How to add a suffix to the Category (Archive) Title?

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1274068
    James

    I’ve been able to customize my archive/category pages to my liking using the blog module of GP Premium. There’s one last thing I’d like to change on these pages and that is add a single word to the end of each title. I’ve searched but can only find a way to prefix the title. Is there a way to add a suffix (for example using code snippets to add to functions.php)?

    This is an example of what I’d like to do:

    Category name ==> What I’d like to show as the category title on my archive pages

    PPC ==> PPC Services
    SEO ==> SEO Services
    Digital Marketing ==> Digital Marketing Services
    Web Analytics ==> Web Analytics Services

    I don’t want to add “Services” to each category name as it’s kind of redundant when it shows in the category widget in the sidebar for example. I have 20 categories and want to add the same word to the end of each title on archive pages.

    Thanks in advance!

    #1274566
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    I believe this is the filter you’re looking for: https://developer.wordpress.org/reference/hooks/get_the_archive_title/

    For example:

    add_filter( 'get_the_archive_title', function( $title ) {
        if ( is_category( 'PPC' ) ) {
            return $title . ' Services';
        }
    
        return $title;
    }, 20 );
    #1275335
    James

    Worked like a dream, thanks. Didn’t realise it was so simple!

    #1275493
    Marcel

    Thank you Tom for the snippet. Could I have a hint from your side how to create a prefix instead of a suffix for that?

    #1275607
    Leo
    Staff
    Customer Support

    Does this work?

    add_filter( 'get_the_archive_title', function( $title ) {
        if ( is_category( 'PPC' ) ) {
            return 'Services ' . $title;
        }
    
        return $title;
    }, 20 );
    #1275663
    Tom
    Lead Developer
    Lead Developer

    Gotta love filters ๐Ÿ™‚

    #1276587
    Marcel

    Thank you, Leo. It is working.
    Tom, you are right, I am gonna love filters. They are so powerful. One more question I would have: where can I find some documentation to learn about the values of arguments the function accepts. On the snippet provided value is “20”. I would like to understand how to use the correct values.
    Thank you very much!

    #1277312
    Tom
    Lead Developer
    Lead Developer

    The WordPress documentation for functions is pretty awesome: https://developer.wordpress.org/reference/functions/add_filter/

    Let me know if you need more info ๐Ÿ™‚

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