Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

WordPress: Displaying Parent Categories Only & Putting Categories Into A Foreach Loop

I’ve been working with manipulating WordPress Categories a lot recently, so I’m just going to quickly jot down a few notes, as I’m sure I’ll need these in the future.

From what i’m aware, currently, WordPress doesn’t allow enough flexibility when it comes to manipulating categories, especially when it comes to identifying the difference between a “Parent” and “Child” category. Let me explain…

The “Depth” Parameter

WordPress only recently added a “depth” parameter, which allows you to pull out ONLY parent or child categories. However, it still isn’t flexible enough because you can only use it with limited functions.

1
2
3
4
<ul>
<?php
wp_list_categories('depth=1'); ?>
</ul>

The above code will list all parent categories in a list format. The downside of this approach? It’s not flexible enough because it lists the parent categories as links. But if that’s all you’re after, that’s cool. But If you want more flexibility with categories, that won’t help.

Putting Categories Into a Foreach Loop And manipulating

The solution to the problem above is as follows:

1
2
3
4
5
6
7
8
9
$categories = get_categories();
foreach ($categories as $cat)
{
  if($cat->parent < 1)
  {
    echo $cat->category_nicename
    echo $cat->cat_name ;
   }
}

The above code will put the categories into an array, meaning you can now pass the values through a foreach loop (as shown in the code), consequently, you can completely control how you want the categories to display. In the example, I’m only printing the category name and category nice name (category url). But you can print out a lot more, which I explain a bit further down

The $cat->parent variable prints the parent of each category. If the category doesn’t have a parent, it has a value of 0. So by adding the if($cat->parent < 1) condition, we prevent all child categories from displaying.

To view all the values in the array or further possible manipulation, print the following array:

1
print_r($categories);

I hope that’s been useful :)

30 Jun 2009 / 6 Comments / Wordpress / by Maruf

6 Comments

  1. Manoj Kumar Bhujabal
    09/07/2009
    1

    Greate script, Thanks………

  2. Jason
    25/07/2009
    2

    Nice post. I am wondering if you possibly know how to just disable the category parent links but still display the name while the subcategories remain intact and in normal behavior?

  3. Kamel
    20/08/2009
    3

    Like Jason, I will be pleased to know how to make the parent level of my categories menu unclickable.

    Regards

  4. awls99
    12/05/2010
    4

    I’m not sure if this was around at the time of this writting, but this will get you there faster:

    $categories=get_categories(array(‘parent’=>0,’exclude’=>’1′));

    the array on the function are the arguments that can be passed, I used exclude cat 1 bacouse it’s generaly the “No category” category, the parent => 0 makes it that it only get category withou parents, thus making the if clause you use unencessary. hope it was clear and useful

  5. Jason
    16/05/2010
    5

    Thanks. I can’t remember what I did because it was a while ago but I did end up solving it. Thank you for your solution awls99. I’ll be sure to try this out in the future.

  6. niv
    16/10/2010
    6

    Hi, how can I use this code to check if there are category child or not and echo an appropriate response?

    I need to output a div only if there are child category for the category I see.

Leave a Reply

© 2012 BrightCherry :)