Archived topic
How to add a suffix to the Category (Archive) Title?
7 replies · Started by James on May 7, 2020
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!
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 );
Worked like a dream, thanks. Didn't realise it was so simple!
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?
Does this work?
add_filter( 'get_the_archive_title', function( $title ) {
if ( is_category( 'PPC' ) ) {
return 'Services ' . $title;
}
return $title;
}, 20 );
Gotta love filters :)
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!
The WordPress documentation for functions is pretty awesome: https://developer.wordpress.org/reference/functions/add_filter/
Let me know if you need more info :)