Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got posts that I want to echo after the post content the categories only if they're a child of a given parent category.

Example:

Parent category: Goal
Child categories of "Goal": brand awareness, brand engagement

If a post is categorized, I want it to echo out those category or categories:

<?php
    $categories = get_the_category();
    $seperator = ' ';
    $output = '';
    if($categories)
    {
        foreach($categories as $category) 
        {
            $output .= '<a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$seperator;
        }
        echo trim($output, $seperator);
    }
?>

I'm using this, but it obviously outputs all categories, rather than just the ones that are children of the "goal" slug.

Edit: Current setup looks like this, but doesn't seem to be working. If I use the ID, it's fine:

<?php
        $categories = get_the_category();
        $seperator = ', ';
        $output = '<strong>GOAL:</strong> ';
        $category = get_category_by_slug('goal');
        if($categories)
        {
            foreach( $categories as $category ) 
            {
            if ( $category->parent == $category->term_id )
                $output .= '<a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$seperator;
        }
        echo trim($output, $seperator);
}
?>
share|improve this question

4 Answers 4

up vote 1 down vote accepted

Change:

$goal = get_category_by_slug('goal');
foreach($categories as $category) 
{
    $output .= '<a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$seperator;
}

Into:

foreach( $categories as $category ) {
    if ( $category->parent == $goal->term_id)
        $output .= '<a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$seperator;
}

Where you replace '123' with the ID of the Goal category.

share|improve this answer
    
Works like a charm. Thanks! –  Keefer Aug 14 '12 at 15:19
2  
I had posted an answer where I used get_categories to get all Goals children using get_category_by_slug to grab goals ID. I would say this is much simpler and better - except the potential pitfall of hardcoding the term_id. Usually development environments are not the production environment - so you dont know the term_id before hand. So I still would suggest using get_category_by_slug('goal')->term_id to retrieve the ID. –  eddiemoya Aug 14 '12 at 15:30
1  
You're right. When it is a an production environment and the term ID is available, hardcoding might be better for perfomance reasons. –  Jasper Denkers Aug 14 '12 at 16:34
    
@eddiemoya How do I update the above code to incorporate the get_category_by_slug('goal')->term_id instead of the ID? –  Keefer Aug 14 '12 at 18:14
1  
replace '123' in my code with $category->term_id and place $category = get_category_by_slug('goal'); before the if statement. –  Jasper Denkers Aug 14 '12 at 18:56

I agree with the above answer except that it should use get_category_by_slug() instead of hardcoded ID's. The comments got sort of confusing to follow because the question was also changed - so I've split this out into my own answer.

To work this into the bit of logic you have above...

<?php
    $goal_id = get_category_by_slug('goal');

    $categories = get_the_category();
    $seperator = ' ';
    $output = '';
    if($categories)
    {
        foreach($categories as $category) 
        {
            if($category->parent == $goal->term_id){
                $output .= '<a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$seperator;
            }
        }
        echo trim($output, $seperator);
    }
?>

I am assuming that the slug of your "Goal" category is simply goal.

share|improve this answer

What about using a conditional check?

here's example which checks for parent category with ID (4).

Example -

//To show child  of cat with id - 4
if($category->category_parent == '4') 
    $output .= 'foo';
share|improve this answer
    
Excuse me, you where quicker. Same solution. Btw, $category->category_parent is the same as $category->parent. –  Jasper Denkers Aug 14 '12 at 15:16
    
taking my words back... –  amit Aug 14 '12 at 15:24

you could use get_the_category_list('',$category_id,$optional_post-ID)

http://codex.wordpress.org/Function_Reference/get_the_category_list

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.