繁体   English   中英

获取帖子中的类别,PHP

[英]get category in posts, php

我尝试为php写一个简单的代码,它可以显示日期,发布身份验证和类别,但我不能显示类别。 我在哪里可以添加? 这是代码:

$posted_on = sprintf(
    esc_html_x( 'Posted on111 %s', 'post date',  'sanse' ),
    '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
);

$byline = sprintf(
    esc_html_x( 'by %s', 'post author', 'sanse' ),
    '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);

在have_posts循环中,可以使用get_the_category()函数显示帖子的类别。

$categories = get_the_category();
if ( ! empty( $categories ) ) {
    echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
}

https://developer.wordpress.org/reference/functions/get_the_category/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM