WordPress archive titles come with a prefix: “Category: News”, “Tag: Recipes”, “Author: Jane”. When you build your own archive template you often want only the name, in your own heading, without changing the archive title everywhere else on the site.
This snippet prints the name of the current archive, escaped, in a heading you choose. It works on category, tag and custom taxonomy archives, post type archives, author archives and the blog page, and prints nothing anywhere else.
If you want to remove the prefix from every archive title instead, including the ones your theme already prints, use Remove Category: prefix from WordPress archive titles. That filter is the better choice when you don’t build the heading yourself.
The snippet
<?php
/**
* The name of the current archive, without "Category:" or any other prefix.
* Scripts Organizer: Trigger location Conditions, Script location Shortcode.
*/
$dpc_tag = 'h1'; // h1-h6, p, span or div.
$dpc_before = ''; // Optional text in front, e.g. 'Topic: '.
$dpc_name = '';
if ( is_category() || is_tag() || is_tax() ) {
$dpc_name = single_term_title( '', false );
} elseif ( is_post_type_archive() ) {
$dpc_name = post_type_archive_title( '', false );
} elseif ( is_author() ) {
$dpc_name = get_the_author_meta( 'display_name', get_queried_object_id() );
} elseif ( is_home() && ! is_front_page() ) {
$dpc_name = get_the_title( (int) get_option( 'page_for_posts' ) );
}
if ( '' !== (string) $dpc_name ) {
$dpc_tag = in_array( $dpc_tag, array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'div' ), true ) ? $dpc_tag : 'h1';
printf(
'<%1$s class="dpc-archive-name">%2$s</%1$s>',
$dpc_tag,
esc_html( $dpc_before . wp_strip_all_tags( $dpc_name ) )
);
}
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Conditions, tick Shortcode under Script location (leave PHP unticked), paste the code into the PHP editor, keep Template on All and publish.
The block now shows its shortcode, for example [scorg_shortcode id="123"]. Paste it where the output should appear: a Shortcode block in the post or in a block theme template, or the shortcode element of your page builder’s single or archive template.
Put it in the archive template, where your heading should be. You can also set Template to Taxonomy and pick the taxonomies, so the shortcode only prints on those archives. Scripts Organizer includes this file every time the shortcode renders, inside its own function. That is why the code has no function declarations (a second shortcode on the same page would fatal with “Cannot redeclare”), uses get_the_ID() or get_queried_object_id() instead of a bare $post, and prefixes its variables with dpc_.
How it works
single_term_title()returns the name of the current category, tag or custom taxonomy term,post_type_archive_title()the post type’s plural name, and the author archive uses the author’s display name.- The heading tag is checked against a short list before it is printed, and the name goes through
esc_html(), so a term called “Tom & Jerry” prints correctly and a name with HTML cannot break the page. - Date archives and search results have no single name, so nothing is printed there. Your template can show its own text for those.
Customise it
- Your own prefix, like the old
single_cat_title( 'Some text: ' ): set$dpc_beforeto'Some text: '. - Different element: set
$dpc_tagtoh2,porspan. - Description below the name: add
echo wp_kses_post( term_description() );at the end.
Gotchas
- In block themes the Query Title block has a Show archive type in title toggle that does the same per template without code.
- On the blog page this prints the title of the page set under Settings → Reading → Posts page; on a front page that lists posts it prints nothing.
In the Scripts Organizer library Import it with one click from Scripts Organizer.