WP获取分类的链接

我正在使用以下变量来显示类别名称。

<?php $postcat = get_the_category( $post->ID ); ?>

<?php echo esc_html($postcat[0]->name );  ?>

我如何获得类别的链接?

示例:www.example.com/category/business


#1


很简单, 你可以使用get_category_link()函数。

// Get the URL of this category
$category_link = get_category_link( $category_id );

#2


请使用以下代码, 它将为你完成工作:

   $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://www.srcmini02.com/65107.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?