0

I have 15 categories. I list this using <div class="category_list"><ul><li><?php wp_list_categories();?></li></ul></div> Now it's listing all 15 categories into a single div which is category_list.

I am trying to print these categories 5 by 5 in three divs. like:

<div class="category_list">first 5 categories here </div>

<div class="category_list">6th to 10th categories here </div>

<div class="category_list">11th to 15th categories here </div>

Those divs are float next to each other I can do that using CSS.
But how can I achieve that HTML markup using PHP coding?

Any help will be very much appreciated. Thanks in advance.

  • Why not have a look at get_categories. This should give you more flexibility – Pieter Goosen Jun 25 '14 at 7:05
4

You can use get_categories() function.

<?php
  $cats =  get_categories();
  $count = 15;  // Total Categorie
  $linetotal=round($count/3); ?>  // Part of Categorie

  <div class="category_list">
    <ul><?php
      $totalcount = 1;
      $totalpart = 1;
      foreach($cats as $cat): ?>
        <li><?php echo $cat->name; ?></li><?php
        if($totalcount % $linetotal == 0 && $totalpart < 3) { 
          echo '</ul></div><div class="category_list"><ul>';
              $totalpart++;
        }
        $totalcount++;
      endforeach;?>
    </ul>
  </div>
  • Thank u so much for this. It's working fine if total category is 15. I'm trying to assign value to $count based on the number of categories. <? $date_arch = wp_get_archives('echo=0'); $date_arch = explode( '<li>' , $date_arch ); $date_arch_count = count($date_arch); echo $date_arch_count;?> is echoing total number of categories. But <? $cats = get_categories('echo=0'); $cats = expload( '<li>', $cats); $cats_count = count($cats); echo $cats_count; ?>says Call to undefined function expload() if we can get this value like $cats_count we can assign value to $cats.any idea? – Riffaz Starr Jun 25 '14 at 8:17
1

wp_list_categories() is default wordpress function to show categories. It itself append the html with category items and print it accordingly.

Now if you want to show categories in 3 div grid you need to print it with your custom code. First load all the terms of category type then

    $terms=get_terms( 'categories', array('orderby' => 'slug', 'hide_empty' => false,'depth' => 3));

echo "<pre>"; print_r($terms); echo "</pre>"; die;

It will display you category array. Now you can traverse this array to print it in any format.

Or you can use the same function to print category 3 times in all the3 three divs.Just specify the particular ids (excluding those 10 that you don't want)

wp_list_categories('show_count=0&use_desc_for_title=0&title_li=0&exclude=1, 16, 30, 31, 32, 78, 80, 9');

Here enter those 10 category ids that you want the first div then exclude other 10 that you don't want in the second div, same for the 3rd div

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.