首页 > 解决方案 > 如何将可点击的标签和类别添加到 WordPress 帖子?

问题描述

我想将可点击的Tags和添加Category到我WordPress的帖子模板中。我使用插件PHP在 WordPress 帖子中显示。

这是我的代码:

$posttags = get_the_tags();
if ($posttags) {
    foreach($posttags as $tag) {
        echo $tag->name.'    '; 
    }
}

但我想显示可点击的标签和类别。PHP我应该使用什么代码?

标签: phpwordpresstagscategories

解决方案


您可以使用get_tag_link获取标签的链接。看下面的例子:

$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    $tag_link = get_tag_link($tag->term_id);
    echo '<a href="' . $tag_link . '">' . $tag->name . '</a>&nbsp;&nbsp; '; 
  }
}

查看 WordPress 参考:https ://codex.wordpress.org/Function_Reference/get_the_tags

显示类别:

$categories = get_categories( array(
    'orderby' => 'name',
    'order'   => 'ASC'
) );

foreach( $categories as $category ) {
    $category_link = '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . $category->name . '</a>';

    echo $category_link;
} 

参考:https ://developer.wordpress.org/reference/functions/get_categories/


推荐阅读