【问题标题】:How can I add clickable tags and category to WordPress post?如何将可点击的标签和类别添加到 WordPress 帖子?
【发布时间】:2019-01-07 00:07:10
【问题描述】:

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

这是我的代码:

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

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

【问题讨论】:

    标签: php wordpress tags categories


    【解决方案1】:

    您可以使用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/

    【讨论】:

    • 谢谢!也许您知道如何以类似的方式添加类别?
    • 您可以像处理标签一样使用get_categories函数并显示,并使用get_category_link获取类别链接
    • 可以只显示这个帖子类别吗?不是所有类别?
    • 使用get_the_category 获取与当前帖子相关的类别列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多