Roger Stringer   About ▾

I'm Roger Stringer: a father, writer, developer, consultant, chef, speaker. Founder of TheInterviewr.

Monthly Archives: May 2010


Quick Function: Check for parent category in wordpress

 

When I was working on foodjumper, there were times when I wanted to check the parent category, for example, if you’re in the Dinner category or viewing a post in that category, I want to display the proper layout for posts in the Recipes category. One function that works, is to do a check like this:

function parent_category( $cats, $_post = null ){
    if( is_string($cats) )  $cats = get_cat_ID($cats);
    foreach ( (array) $cats as $cat ) {
        $descendants = get_term_children( (int) $cat, 'category');
        if ( $descendants && in_category( $descendants, $_id ) ) return true;
    }
    return false;
}

WordPress published a similar function on their site, but it used the actual category ID in the function. I found it easier to let you enter the slug instead. Now, when you want to check if a post is in a category that is a child, you can do a check like this:

if(in_category('recipes') || parent_category('recipes') ): 
    // display this post as a recipe
else: 
    // display this post as a normal post
endif;

This can be adapted to almost any type of similar treatment, I just used recipes above as an example since it’s where I just used this function.

Filed Under: Code

~•~

Foodjumper.com, new foodie blog

 

foodjumpercom-foodie-blog.txt

Today, I’m pleased to announce the launch of FoodJumper.com. FoodJumper.com is the end result of a month of planning, before even starting down the road of adding recipes and posts. To put it simply, it’s a foodie blog, similar to foodizu.com, but different in that, this one is just me and also contains posts and videos, etc, whereas foodizu is a community of people adding recipes. Why make another foodie site? Well, to put it simply, because I love food, so why not?

Filed Under: Articles

~•~