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

customization - How can I add category variable inside posts?

programmeradmin0浏览0评论

I want to add a category variable along with the date variable that post has been published.

For example, when I go to a post, I see the following under the post title:

last updated on May 10, 2019

I want to add the category name that this article has been posted to. For example, if I add this post under news category, it will show the category along with date as a link for this category:

last updated on May 10, 2019 in news

So far I know the code that we should modify in templates-tags.php file especially:

printf(
                '<span class="posted-on">last updated on %1$s</span><span class="byline"> <i class="fa fa-user"></i> %2$s</span> in ',
                sprintf(
                    '<a href="%1$s" rel="bookmark">%2$s</a>',
                    esc_url( get_permalink() ),
                    $time_string
                ),
                sprintf(
                    '<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>',
                    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
                    esc_html( get_the_author() )
                )
            );
    }
endif;

I want to add a category variable along with the date variable that post has been published.

For example, when I go to a post, I see the following under the post title:

last updated on May 10, 2019

I want to add the category name that this article has been posted to. For example, if I add this post under news category, it will show the category along with date as a link for this category:

last updated on May 10, 2019 in news

So far I know the code that we should modify in templates-tags.php file especially:

printf(
                '<span class="posted-on">last updated on %1$s</span><span class="byline"> <i class="fa fa-user"></i> %2$s</span> in ',
                sprintf(
                    '<a href="%1$s" rel="bookmark">%2$s</a>',
                    esc_url( get_permalink() ),
                    $time_string
                ),
                sprintf(
                    '<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>',
                    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
                    esc_html( get_the_author() )
                )
            );
    }
endif;
Share Improve this question edited May 20, 2019 at 0:04 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked May 19, 2019 at 23:10 iLinux85iLinux85 1274 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use get_the_category_list(), which returns a list of categories assigned to the post (each category in the list links to the category archive):

printf(
    '<span class="posted-on">last updated on %1$s</span><span class="byline"> <i class="fa fa-user"></i> %2$s</span> in %3$s',
    sprintf(
        '<a href="%1$s" rel="bookmark">%2$s</a>',
        esc_url( get_permalink() ),
        $time_string
    ),
    sprintf(
        '<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>',
        esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
        esc_html( get_the_author() )
    ),
    get_the_category_list( ', ' )
);
发布评论

评论列表(0)

  1. 暂无评论