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 custom post type (photos) with a custom taxonomy (categories) and I'd like to display the taxonomy list in my sidebar in two columns. I have a modified snippet that I found elsewhere to do this, but it doesn't generate the current class for the particular category it is on. Maybe it is not the best way to list the taxonomy to begin with, so I hope someone will suggest a better solution.

Here's what I have now:

<?php $term = get_term_by( 'name', 'categories', 'photos' );
      $cat_term_id = $term->term_id;

$cats = explode("<br />",wp_list_categories("title_li=&echo=0&child_of=$cat_term_id&depth=1&style=none&taxonomy=categories"));
$cat_n = count($cats) - 1;
for ($i=0;$i<$cat_n;$i++):
if ($i<$cat_n/2):
$cat_left = $cat_left.'<li>'.$cats[$i].'</li>';
elseif ($i>=$cat_n/2):
$cat_right = $cat_right.'<li>'.$cats[$i].'</li>';
endif;
endfor;
?>
<ul class="left">
<?php echo $cat_left;?>
</ul>
<ul class="right">
<?php echo $cat_right;?>
</ul>

Thanks!

share|improve this question
add comment

4 Answers 4

As a follow up to @Rutwick Gangurde Answer, here's an example.

Notes:

  • Put it in your functions.php file, call it in the template file.
  • Alter the output by inspecting the $term inside the foreach loop
  • Read the comments inside the function

-

    function wpse25433_terms_list( $cat = array('categories'), $el = 'li', $echo = true )
    {
    global $post;
    $post_id = $post->ID;

    $terms = wp_get_object_terms( 
        $post_id, 
        $cat, 
        array( 
             'orderby' => 'name'
            ,'order'   => 'ASC'
            ,'fields'  => 'all' 
    );

    $counter = count ( $terms );
    $i = 1;
    $left = $right = '';
    foreach ( $terms as $term )
    {
        # uncomment the following line to inspect the $term object
        // echo '<pre>'; print_r( $term ); echo '</pre>';
        $i++;
        if ( $i < $counter / 2 )
            $left .= ''; // @example: $term['name'];
        else 
            $right .= ''; // @example: $term['whatever_value_you_need'];
        $i++;
    }

    $el_cont = 'ul';
    if ( $el !== 'li' )
        $el_cont = $el = 'div';
    $output  = "<{$el_cont} class='terms-container'>";
    $output .= "<{$el} class='terms-left'>{$left}</{$el}>";
    $output .= "<{$el} class='terms-right'>{$right}</{$el}>";
    $output .= "</{$el_cont}>";

    if ( $echo )
        return print $output;

    return $output;
}

// Use it in your template like this:
wpse25433_terms_list( array( 'name', 'some', 'taxonomies' ), 'li or div', true );
share|improve this answer
    
Thanks, but I'm afraid I doesn't work for me. I don't understand very much from it (noob syndrome!), but I think you tried to call the post categories. I have a taxonomy called 'categories' that is attached to 'photos' and is separate from the normal blog categories. Please correct me if I'm wrong. You can see my response to Rutwick Gangurde as it partially solved the issue but failed to generate the columns. Thanks for your help. –  notabot Aug 9 '11 at 18:21
1  
The "categories" are added via the functions arguments and then called inside wp_get_object_terms() (as described in the Codex). Please just "read" the function and the comments I wrote. Then try it, come back and report with an output or an error message. –  kaiser Aug 9 '11 at 22:09
add comment

This is one way I made it work (please check the image attached). Just that I made it as a shortcode, you can use the function/code as you want. Also replace the taxonomy with yours, I used mine!:

add_shortcode('dis', 'displayascols');
function displayascols()
{
    $cats = get_terms('companycategory', array('hide_empty'=>0));
    $l = count($cats);

    if($l%2 == 1)
    {
        $l_even = $l-1;
        $l_half = $l_even/2;

        $cat_1 = array_slice($cats, 0, $l_half);
        $cat_2 = array_slice($cats, $l_half);
    }
    else
    {
        $l_even = $l/2;

        $cat_1 = array_slice($cats, 0, $l_even);
        $cat_2 = array_slice($cats, $l_even);
    }

    $cat_left = '';
    $cat_right = '';

    for ($i=0; $i<count($cat_1); $i++){
        $cat_left .= '<li><a href='.$cat_1[$i]->slug.'>'.$cat_1[$i]->name.'</a></li>';
    }

    for ($i=0; $i<count($cat_2); $i++){
        $cat_right .= '<li><a href='.$cat_2[$i]->slug.'>'.$cat_2[$i]->name.'</a></li>';
    }
    ?>

    <ul class="left" style="float: left;">
        <?php echo $cat_left;?>
    </ul>
    <ul class="right" style="float: right;">
        <?php echo $cat_right;?>
    </ul><?php
}

Let me know!here's the output

share|improve this answer
    
I'm experiencing a strange behavior with this one. The columns are showing, but it displays duplicate categories side by side, and instead of links I get plain text. Hmmm, any ideas? Thanks for your help thus far! –  notabot Aug 10 '11 at 8:00
    
You're welcome! 1) The columns are showing, but it displays duplicate categories side by side - Can you please post a screen shot demonstrating this? 2) instead of links I get plain text - I updated the code in my original answer, please check the code. –  Rutwick Gangurde Aug 10 '11 at 8:15
    
Thanks! I've updated the code and the links are showing correctly, but the duplicate categories are still there. Screenshot: i.imgur.com/QTCn3.png –  notabot Aug 10 '11 at 8:26
    
Ahh... I had checked only if the count was odd, I've updated the code to check if count is even too. Please check the original code now. It should work now. –  Rutwick Gangurde Aug 10 '11 at 10:02
    
Thanks Rutwick, this solved the issue. Now I see there is a problem with the permalink generation. For example, when on homepage, the category link looks like this: domain.com/animals instead domain.com/photos/animals . When I'm on domain.com/photos the links look correct, but when I go to domain.com/photos/animals the next links look like this: domain.com/photos/animals/people. When on a single post type domain.com/photo/tropical-bird it generates: domain.com/photo/tropical-bird/people. I'm Sorry for all the trouble and hope this can be solved. –  notabot Aug 10 '11 at 16:54
show 1 more comment

You can use wp_get_object_terms. It returns an array, leaving you with a better control over the display.

share|improve this answer
    
Thanks. I've had partial success with your suggestion, except now it doesn't split up the list in columns. Here is the code I used: pastebin.com/0pM7uqjj Any suggestions on how to fix this? –  notabot Aug 9 '11 at 17:30
    
Try passing echo=0 in args. Setting echo to 0 will return the string as a variable so that explode can split it on '<br />' tags. It's 1 by default, means 'print it right when the function is called'. Let me know if it worked while I try your example locally. –  Rutwick Gangurde Aug 10 '11 at 2:43
    
See my new answer please! –  Rutwick Gangurde Aug 10 '11 at 3:48
    
Unfortunately this didn't work. Setting a value of '0' doesn't print anything at all, while '1' just does the one column as previously. Could you please take a look at the updated snippet to see if I did it right pastebin.com/y2nSErqs –  notabot Aug 10 '11 at 7:04
    
Hey didn't you check my new answer? I posted a whole working example with a screen-shot of the output man! Please try using that! All you need to do is replace the taxonomy name with your taxonomy name! wordpress.stackexchange.com/questions/25433/… –  Rutwick Gangurde Aug 10 '11 at 7:32
add comment

You can do it with little CSS, assuming you starting with a classical list (<ul class="foo"><li></li></ul>) :

.foo {
    -moz-column-count: 2;
    -moz-column-gap: 10px;
    -webkit-column-count: 2;
    -webkit-column-gap: 10px;
    column-count: 2;
    column-gap: 10px;
}

For older browser just make the li float.

share|improve this answer
add comment

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.