Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
        ?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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;}
share|improve this answer
    
Works perfectly (ignore my previous content, found the problem). Thanks again for your help. –  user1255049 Oct 27 '13 at 19:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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