0

I have a section in my sidebar where I'd like to display my most popular tags in two columns. I found this post from WPSnipp that details how to narrow your list down to only the most popular, however I'm wondering how I can arrange them to appear in two columns - 10 on the left, and 10 on the right in alphabetical order.

Here's what I'm using: functions.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>";
                }
        }
}
?>

sidebar-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 Answer 1

1

In my opinion you are doing it so complicated.

I would rather do a classic query and then through css it made into 2-column.

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;
         }
}

and your css

.top_tags li {display:block; float:left; width:50%; clear:right;}
1
  • Works perfectly (ignore my previous content, found the problem). Thanks again for your help. Oct 27, 2013 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.