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

Image frame
Wordpress: Displaying Parent Categories Only & Putting Categories Into A Foreach Loop
Maruf
Jun 30th, 2009
Maruf scribbled this post.

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


Filed away: Wordpress

comments

Image frame
1

Greate script, Thanks………

Manoj Kumar Bhujabal
Jul 9th, 2009
Image frame
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?

Jason
Jul 25th, 2009
Image frame
3

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

Regards

Kamel
Aug 20th, 2009

feel free to leave a scribble

Name:
Email:
gravatar
What an image next to your comments?
visit gravatar.com
Message:
want a website, do ya?

latest projects

Image frame
RG Active
view site- RG Active
Image frame
Property Investment Project
view site- Property Investment Project

latest scribbles

latest comments

zurin Gravatar frame
zurin said
hi, i’m having problem with this function. it works fine...
Karlos Gravatar frame
Karlos said
Awesome awesome awesome :) Just started re-designing...
satheesh Gravatar frame
satheesh said
It solved my problem. Thank you. Is it possible to get the...

blog categories

Get a free quote