The WordPress SEO plugin by Yoast allows users to add SEO titles and meta descriptions to taxonomy term archive pages. These are then used in the head of the document.
I´m trying to display the SEO title of taxonomy terms as an H1 in my taxonomy archive template.
To do this in a post, post type or page is easy:
echo get_post_meta($post->ID, '_yoast_wpseo_title', true);
On archive templates this doesn´t work.
Does anyone know how to get this to work?
Screenshot example
This is the title of a specific tag term. I´m trying to display this title – but then for a custom taxonomy term – in my archive templates.
Final code
This is what I ended up using in my archive.php
template. It works if you´re using a custom taxonomy. For tags or categories have a look at Mike Madern´s answer below.
<h1 class="archive-title">
<?php
if ( is_tax() ) :
$taxonomy = get_queried_object()->taxonomy;
$term_id = get_queried_object()->term_id;
$meta = get_option( 'wpseo_taxonomy_meta' );
$title = $meta[$taxonomy][$term_id]['wpseo_title'];
//printf( '<pre>%s</pre>', print_r( get_option( 'wpseo_taxonomy_meta' ), 1 ) );
if ( isset($meta) && !empty($title) ) :
echo apply_filters( 'the_title', $title );
else :
single_term_title();
endif;
endif;
?>
</h1>