在两列中显示最受欢迎的标签

时间:2013-10-27 作者:user1255049

我在侧边栏中有一个部分,我想在两列中显示我最常用的标签。我发现this post WPSNiP详细介绍了如何将列表缩小到最受欢迎的列表,但我想知道如何将它们分为两列—10列按字母顺序排列在左侧,10列按字母顺序排列在右侧。

我使用的是:函数。php

<?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>";
                }
        }
}
?>
提要栏-r.php

<h2 class="widgettitle">#HASHTAG</h2>
        <? $tags = top_tags();
        $tags_count = count($tags);
        $number = ceil($tags_count / 2);
        $args = array(\'number\' => $number);
        $tagsleft = get_tags($args);
        $args = array(\'number\' => $number, \'offset\' => $number);
        $tagsright = get_tags($args);

        $html = \'<ul class="catleft">\';
            foreach ($tagsleft as $tag){
                $tag_link = get_tag_link($tag->term_id);

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

            $html .= \'<ul class="catright">\';
            foreach ($tagsright as $tag){
                $tag_link = get_tag_link($tag->term_id);

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

1 个回复
最合适的回答,由SO网友:Ivan Hanák 整理而成

在我看来,你做这件事太复杂了。

我宁愿做一个经典的查询,然后通过css将其制作成2列。

function top_tags() {
    $tags = get_the_tags();

        if($tags)
        {
             $output = \'<h4>Top tags</h4> <ul class="top_tags">\';
             foreach($tags as $tag)
                   $output .= \'<li><a href="\'.get_tag_link($tag->term_id).\'" title="\'.$tag->name.\'">\'.$tag->name.\'</a></li>\';

             $output .= \'</ul>\';

             echo $output;
         }
}
还有你的css

.top_tags li {display:block; float:left; width:50%; clear:right;}

结束

相关推荐

如何在插件的uninstall.php中删除自定义分类术语?

我正在编写插件的卸载。php文件,并希望它删除插件自定义分类法中创建的任何术语。在插件的卸载中。我正在使用的php文件:// Delete all custom terms for this taxonomy $terms = get_terms(\'custom_taxonomy\'); foreach ($terms as $term) { wp_delete_term( $term->ID, \'custom_taxonomy\' ); } 问