简体   繁体   English

如何从 Wordpress 页面/帖子中获取所有标签作为短代码列表?

[英]How to get all tags from Wordpress page/post as a list in shortcode?

At the end of each page and every post, I would like to output the tags as a list in a shortcode.在每个页面和每个帖子的末尾,我想将标签输出为短代码中的列表。

Unfortunately, I do not know much about PHP, but someone who can understand will definitely be able to correct my mistake using the code below.不幸的是,我对 PHP 了解不多,但是可以理解的人肯定能够使用下面的代码纠正我的错误。

Thank you in advance!提前谢谢你!

<?php // functions.php | get tags
function addTag( $classes = '' ) {

    if( is_page() ) {
        $tags = get_the_tags(); // tags
        if(!empty($tags))
        {
            foreach( $tags as $tag ) {
                $tagOutput[] = '<li>' . $tag->name . '</li>';
            }
        }
    }
    return $tags;

}
add_shortcode('tags', 'addTag');

The method needs to return a string to be able to print any markup.该方法需要返回一个字符串才能打印任何标记。

"Shortcode functions should return the text that is to be used to replace the shortcode." “短代码函数应该返回用于替换短代码的文本。”https://codex.wordpress.org/Function_Reference/add_shortcodehttps://codex.wordpress.org/Function_Reference/add_shortcode

function getTagList($classes = '') {
    global $post;
    $tags = get_the_tags($post->ID);
    $tagOutput = [];

    if (!empty($tags)) {
        array_push($tagOutput, '<ul class="tag-list '.$classes.'">');
        foreach($tags as $tag) {
            array_push($tagOutput, '<li>'.$tag->name.'</li>');
        }
        array_push($tagOutput, '</ul>');
    }

    return implode('', $tagOutput);
}

add_shortcode('tagsList', 'getTagList');

Edit: Removed check for is_page since get_the_tags will simply return empty if there aren't any编辑:删除了对is_page检查,因为如果没有, get_the_tags将简单地返回空

Be aware that pages don't have tags unless you've changed something.请注意,除非您更改了某些内容,否则页面没有标签。

That said, this should work.也就是说,这应该有效。 It also adds the <ul> around the tags, but you can change that, and I'm sure you see where.它还在标签周围添加了<ul> ,但您可以更改它,我相信您会看到在哪里。 I've used is_singular , but you'll probably get away without that condition at all, unless you do add [tags] in a custom post type and don't want the output there.我使用过is_singular ,但是您可能is_singular没有这种条件就可以逃脱,除非您确实在自定义帖子类型中添加了[tags]并且不希望在那里输出。 I assume you want to add more modification, otherwise webdevdani's suggestion regarding using the_tags is probably simpler.我假设您想添加更多修改,否则 webdevdani 关于使用the_tags的建议可能更简单。

// get tags 
function addTag( $classes = '' ) {
    if( is_singular("post") || is_singular("page") ) {
        $tags = get_the_tags();
        if(is_array($tags) && !empty($tags)) {
            $tagOutput = array("<ul>");
            foreach( $tags as $tag ) {
                $tagOutput[] = '<li>' . $tag->name . '</li>';
            }
            $tagOutput[] = "</ul>";
            return implode("", $tagOutput);
        }
    }
    return "";
}
add_shortcode('tags', 'addTag');

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

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