How to print Posts tags in single posts head section from function.php.
<meta property="article:tag" content="Single Posts Tags" />
How to print Posts tags in single posts head section from function.php.
<meta property="article:tag" content="Single Posts Tags" />
2 Answers
Reset to default 1You can add content to the head in Wordpress using the wp_head hook, see this link.
This will allow you to write a function to output your content as you wish. This could just be a static piece of text or something created dynamically (e.g. to incorporate a list of tags).
Also take note of the priority value of the add_action() as this will set the output to appear higher or lower in the head section.
Take care to check if there is any accepted formatting for the output into the head first, and also bear in mind that there are some well known SEO plugins that will help with kind of thing if your purpose is to improve SEO for your site.
Here's how I would do it:
function sx372783_post_tags_meta() {
global $post;
// Is this a single post? If not, return.
if ( ! is_single() ) {
return;
}
// Are there any tags? If not, return.
if ( ! $post_tags = get_the_tags( $post->ID ) ) {
return;
}
?>
<meta property="article:tag" content="<?php echo implode( ' ', wp_list_pluck( $post_tags, 'name' ) ); ?>" />
<meta property="article:published_time" content="<?php echo $post->post_date_gmt; ?>" />
<?php
}
add_action( 'wp_head', 'sx372783_post_tags_meta' );