php 如何获取 WordPress 中的所有帖子标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41673373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:26:26  来源:igfitidea点击:

How do I get all the post tags in WordPress?

phpwordpress

提问by theKing

I would like to get all the post tags in my WordPress. Below is my code in the footer:

我想在我的 WordPress 中获取所有帖子标签。下面是我在页脚中的代码:

<?php
global $wpdb;

$tags = get_terms('post_tag');
echo '<ul>';
foreach ($tags as $tag)
{
    echo '<li>' . $tag->name . '</li>';
}
echo '</ul>';
?>

With the above code I am getting only the tags associated with a specific post, not the entire list of tags in WordPress.

使用上面的代码,我只得到与特定帖子相关的标签,而不是 WordPress 中的整个标签列表。

Any help will be appreciated. Thanks.

任何帮助将不胜感激。谢谢。

回答by Rene Korss

Use get_tagsto get all posts tags

使用get_tags获取所有帖子标签

<?php 
$tags = get_tags(array(
  'hide_empty' => false
));
echo '<ul>';
foreach ($tags as $tag) {
  echo '<li>' . $tag->name . '</li>';
}
echo '</ul>';
?>

回答by Bharat Dangar

Try This

尝试这个

$tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
    $tag_link = get_tag_link( $tag->term_id );

    $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
    $html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;

回答by Alfrex92

If someone wants to display the most popular you can use this code

如果有人想显示最受欢迎的,您可以使用此代码

<?php 
        $tags = get_tags(array(
            'smallest'                  => 10, 
            'largest'                   => 22,
            'unit'                      => 'px', 
            'number'                    => 10,  
            'format'                    => 'flat',
            'separator'                 => " ",
            'orderby'                   => 'count', 
            'order'                     => 'DESC',
            'show_count'                => 1,
            'echo'                      => false
        ));
        echo '<ul class="AddYourClassUl">';
        foreach ($tags as $tag) {
        echo '<li class="AddYourClassLi">' . $tag->name . '</li>';
        }
        echo '</ul>';
    ?>