• Hey guys,
    I’m trying to display categories that are grouped by the letter they start with. Eg

    A
    Apples,
    Apricots
    Anjas
    B
    Bananas
    Beats
    C
    etc etc

    I really don’t want to create sub-categories under each letter. Instead, I’d prefer to do this programmatically if possible. My problem is that I’m a complete nubie to php!

    The code below by gcarson shows how to do it perfectly for tags. However, I can’t (for the life of me) figure out how to do the same thing with categories. Can anyone help? Any advice would be much appreciated.

    <?php
    $tags = get_tags( array('name__like' => "a", 'order' => 'ASC') );
    $tags_count = count($tags);
    $count = intval( $tags->count );
    $percolumn = ceil($tags_count / 3);
    
    for ($i = 0;$i < $tags_count;$i++):
        if ($i < $percolumn):
        $tag_left .= '
    	<li><a href="'. get_tag_link($tags[$i]->term_id) . '"rel="tag">' . $tags[$i]->name .' (' . $tags[$i]->count . ')</a></li>
    ' . "\n";
        elseif ($i >= $percolumn && $i < $percolumn*2):
        $tag_mid .= '
    	<li><a href="'. get_tag_link($tags[$i]->term_id) . '"rel="tag">' . $tags[$i]->name .'  (' . $tags[$i]->count . ')</a></li>
    ' . "\n";
        elseif ($i >= $percolumn*2):
        $tag_right .= '
    	<li><a href="'. get_tag_link($tags[$i]->term_id) . '"rel="tag">' . $tags[$i]->name .' (' . $tags[$i]->count . ')</a></li>
    ' . "\n";
        endif;
    endfor;
    ?>
    
    <div class="list">
    <ul>
    <?php echo $tag_left; ?>
    </ul>
    </div>
    
    <div class="listlist">
    <ul>
    <?php echo $tag_mid; ?>
    </ul>
    </div>
    
    <div class="list">
    <ul>
    <?php echo $tag_right; ?>
    </ul>
    </div>

    For reference, this is the post the code came from: http://wordpress.org/support/topic/a-z-index-list-of-tags

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter AmyZealand

    (@amyzealand)

    Thanks vtxyzzy! Thats a really useful bit of code!

    For anyone who is interested, I’ve also (finally) figured out how to make the original code suitable for categories;

    These are categories starting with b
    				<?php
    				$args = array('name__like' => "b", 'order' => 'ASC');
    				$categories = get_terms( 'category', $args );
    				$categories_count = count($categories);
    				$count = intval( $categories->count );
    				$percolumn = ceil($categories_count / 3);
    
    				for ($i = 0;$i < $categories_count;$i++):
    					if ($i < $percolumn):
    					$category_left .= '
    					<li><a href="'. get_category_link($categories[$i]->term_id) . '">' . $categories[$i]->name .' (' . $categories[$i]->count . ')</a></li>
    				' . "\n";
    					elseif ($i >= $percolumn && $i < $percolumn*2):
    					$category_mid .= '
    					<li><a href="'. get_category_link($categories[$i]->term_id) . '">' . $categories[$i]->name .'  (' . $categories[$i]->count . ')</a></li>
    				' . "\n";
    					elseif ($i >= $percolumn*2):
    					$category_right .= '
    					<li><a href="'. get_category_link($categories[$i]->term_id) . '">' . $categories[$i]->name .' (' . $categories[$i]->count . ')</a></li>
    				' . "\n";
    					endif;
    				endfor;
    				?>
    
    				<div class="list">
    				<ul>
    				<?php echo $category_left; ?>
    				</ul>
    				</div>
    
    				<div class="listlist">
    				<ul>
    				<?php echo $category_mid; ?>
    				</ul>
    				</div>
    
    				<div class="list">
    				<ul>
    				<?php echo $category_right; ?>
    				</ul>
    				</div>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display categories that start with a certain letter’ is closed to new replies.