最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add get_the_tags() to Single Posts <head> section via functions.php

programmeradmin0浏览0评论

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" />

Share Improve this question asked Aug 11, 2020 at 9:37 PuneetPuneet 477 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You 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' );
发布评论

评论列表(0)

  1. 暂无评论