【问题标题】:How to get tag_description in foreach Wordpress template?如何在 foreach Wordpress 模板中获取 tag_description?
【发布时间】:2014-10-16 11:26:09
【问题描述】:

我已经制作了这个脚本,

<?php
function top_tags() {
    $tags = get_tags();

    if (empty($tags))
        return;

    $counts = $tag_links = array();
    foreach ( (array) $tags as $tag ) {
        $counts[$tag->name] = $tag->count;
        $tag_links[$tag->name] = get_tag_link( $tag->term_id );
    }

    asort($counts);
    $counts = array_reverse( $counts, true );

    $i = 0;
    foreach ( $counts as $tag => $count ) {
        $i++;
        $tag_link = clean_url($tag_links[$tag]);
        $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
        if($i < 11){
            print "<li><a href=\"$tag_link\">$tag ($count) </a></li>";
        }
    }
}
?>

但我似乎无法获得每个标签的 tag_description。如果我在 foreach 函数中使用$description = tag_description();,它不会显示任何内容。

【问题讨论】:

    标签: php wordpress foreach tags


    【解决方案1】:

    怎么样...这个:

    <?php
    function top_tags() {
        $tags = get_tags();
    
        if (empty($tags))
            return;
    
        $counts = $tag_links = array();
        foreach ( (array) $tags as $tag ) {
            $counts[$tag->name] = $tag->count;
            $tag_links[$tag->name] = get_tag_link( $tag->term_id );
        }
    
        asort($counts);
        $counts = array_reverse( $counts, true );
    
        $i = 0;
        foreach ( $counts as $tag => $count ) {
            $i++;
            $tag_link = clean_url($tag_links[$tag]);
            $tag_description = tag_description(get_term_by('name', $tag, 'post_tag')->term_id);
            $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
            if($i < 11){
                print "<li><a href=\"$tag_link\">$tag ($count) </a> $tag_description </li>";
            }
        }
    }
    ?>
    

    一种更有效的方法(未经测试):

    <?php
    function top_tags() {
        $tags = get_tags();
    
        if (empty($tags))
            return;
    
        $counts = $tag_links = array();
        foreach ( (array) $tags as $tag ) {
            $counts[$tag->name] = $tag->count;
            $tag_links[$tag->name] = array( 'url' => get_tag_link( $tag->term_id ), 'description' => $tag->description);
        }
    
        asort($counts);
        $counts = array_reverse( $counts, true );
    
        $i = 0;
        foreach ( $counts as $tag => $count ) {
            $i++;
            $tag_link = clean_url($tag_links[$tag]['url']);
            $tag_description = $tag_links[$tag]['description'];
            $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
            if($i < 11){
                print "<li><a href=\"$tag_link\">$tag ($count) </a> $tag_description </li>";
            }
        }
    }
    ?>
    

    【讨论】:

    • 不,仍然什么也没看到。不过感谢您的回答。
    • 您尝试过我的编辑吗?在 get_term_by 中标记已更改为“post_tag”
    • 哈哈太棒了,不用担心。
    【解决方案2】:

    只是为了评论您不应该使用的想法,您可以在foreach 循环中使用tag_description,如下所示

    $description = tag_description($tag->term_id);
    

    上面的方法没有错,但是get_tags已经返回了标签描述,你可以返回如下

    $tag->description
    

    编辑

    要查看get_tags 返回的内容,请执行以下操作

    $tags = get_tags;
    ?><pre><?php var_dump($tags); ?></pre><?php
    

    【讨论】:

    • 谢谢彼得。很高兴学习有关这些术语的新知识。贝当克特。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 2012-07-24
    • 2022-08-21
    • 2015-02-20
    • 2015-04-21
    • 2013-06-11
    相关资源
    最近更新 更多