Display Custom Taxonomy in Sidebar in Two Columns

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

Leave a Comment