Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through wordpress categories

I am new to Wordpress and been pulling my hair out trying to create a category loop. The loop is supposed to:

  1. loop through all categories
  2. echo out the category name (with link to
  3. echo out the last 5 posts in that category (with permalink to post)

The html for each would be

<div class="cat_wrap">
   <div class="cat_name">
       <a href="<?php get_category_link( $category_id ); ?>">Cat Name</a>
   </div>
   <ul class="cat_items">
      <li class="cat_item">
         <a href="permalink">cat item 1</a>
      </li>
      <li class="cat_item">
         <a href="permalink">cat item 2</a>
      </li>
      <li class="cat_item">
          <a href="permalink">cat item 3</a>
      </li>
      <li class="cat_item">
         <a href="permalink">cat item 4</a>
      </li>
      <li class="cat_item">
         <a href="permalink">cat item 5</a>
      </li>
   </ul>
</div>

Please help

like image 237
Simon Avatar asked Nov 22 '09 23:11

Simon


1 Answers

I have made this bit of code to loop through the nested categories. Sharing.

        //Start on the category of your choice       
        ShowCategories(0);

        function ShowCategories($parent_category) {
                $categories = get_categories(array('parent' => $parent_category, 'hide_empty' => 0));  
                foreach ($categories as $category) {
                    ?><ul><li><?=$category->cat_name;?><?
                    ShowCategories($category->cat_ID);
                    ?></li></ul><?
                }
        }
like image 146
Etienne Dupuis Avatar answered Oct 30 '22 09:10

Etienne Dupuis