Archived topic
Rendering a shortcode in entry-title on an archive page.
3 replies · Started by Kyle on October 17, 2021
Is there a filter I can use to render a shortcode in posts on the archive page? I have a shortcode to show the current year and it works in the post title on the single page but not in the title on the archive page. I am sure it is a filter or something that I am missing. Below is my current code:
//Shortcode to add the current year into a post title
function year_shortcode () {
$year = date_i18n ('Y');
return $year;
}
add_shortcode ('year', 'year_shortcode');
add_filter( ‘the_title’, ‘do_shortcode’ );
add_filter( ‘wpseo_title’, ‘do_shortcode’ );
add_filter( ‘wpseo_metadesc’, ‘do_shortcode’ );
Thank you in advanced!
Hi Kyle,
For Archive pages, you need the get_the_archive_title filter.
You'll also need to set priority > 10 on it because the theme filters it by default - https://github.com/tomusborne/generatepress/blob/8c12b75d53585b9947fb5954cc111b2f9f9239c5/inc/structure/archives.php#L53-L92
Try this:
add_filter( 'get_the_archive_title' , 'do_shortcode', 15 );
Thank you Elvin. Much Appreciated!
No problem. Glad to be of any help .:D