Adjust WordPress archive headings in the theme
In this tutorial, I will show you how you can customize the WordPress archive headings in the theme according to your wishes. By default, archive titles are displayed on all pages for categories, keywords, authors, and monthly archives.
Since version 4.1 WordPress provides the function the_archive_title
to display the archive headings. For theme developers, there is no need to manually output each title for the various archive pages such as categories and keywords. The_archive_title now takes on this task.
Most current and modern sites should therefore use this feature. Therefore, in many themes, only a common archive.php file is required instead of a category.php, tag.php, author.php and archive.php for the various archives, as was previously the case. An example of this approach is TwentySixteen.
A look behind the scenes of the function
the_archive_title
is just a wrapper function for outputting the archive heading. The actual title of the archive is generated in the get_the_archive_title function. The different types of archive pages are queried in a long if-else branch and the title is created accordingly.
A look at the source code of the function shows us that at the end the title is returned by a get_the_archive_title
filter, which can be used to subsequently adapt the archive headings in a theme or plugin.
Remove "Category:", "Keyword:" and "Author:" from the WordPress archive headings
If you only want to display the name of the category, keyword or author on the archive pages without additional text, you can change the standard functionality of the archive title with a small snippet of code. The function uses the filter mentioned above.
Change WordPress archive headings and use different texts
In addition to removing the standard texts for the archive titles, these can also be changed and renamed. This works with a slightly modified code snippet, which again uses the get_archive_title
filter:
Please note: The two code snippets only work if your theme also uses the function the_archive_title to output the archive headings. I also recommend using the code snippets in the functions.php of a child theme.
* Update: Change archive title without code scraps
In the meantime I have also published a plugin with which the archive headings can be adjusted directly in the WordPress backend - without any code snippets.
More information on this in my post on the Custom Archive Titles Plugin.
The single_cat_title and single_tag_title functions each consist of a single line in which the parameters are passed on to the single_term_title function.
If you were to deal with categories, keywords and terms in one query in example no.2, you could save a few lines.
Just a thought that came to me while reading. In and of itself, the snippet is perfectly fine.
many Greetings
Marvin
Hello Marvin,
Thank you for pointing this out, you are of course right.
I just adopted the if queries including single_cat_title and single_tag_title from the original function and I have to admit that I haven't looked at them closely.
With the direct use of single_term_title, of course, the code can be made a bit more efficient.
Many Greetings,
Brian
You made my day!
After a somewhat unnerved search and “fine-tuning of the Google search”, finally! came across this entry. YEP! For php dummies like me, however, you can't write boldly enough where the code snippets belong (IN FUNCTIONS.PHP !!).
Thank you very much and continue blogging successfully!
Hi there!
I'm glad that the article was helpful 🙂
In the meantime I have also published a plugin for it, with which you can adjust the archive titles in the WordPress backend - without any code snippets.
The name of the plugin is Custom Archive Titles and can be downloaded from https://de.wordpress.org/plugins/custom-archive-titles/ .
I have already presented the plugin here in the blog: https://themekiller.me/2016/12/13/wordpress-archiv-ueberschriften-mit-custom-archive-titles-aendern/
Many Greetings,
Brian