1

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);
}
?>

4 Answers 4

1

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.

7
  • 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
    Commented Aug 14, 2012 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. Commented Aug 14, 2012 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
    Commented Aug 14, 2012 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. Commented Aug 14, 2012 at 18:56
  • @JasperDenkers - I added my newest code above (second snippet in my question). Doesn't seem to be working now. Did I put the if in the wrong place?
    – Keefer
    Commented Aug 14, 2012 at 19:17
1

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.

0

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';
2
  • Excuse me, you where quicker. Same solution. Btw, $category->category_parent is the same as $category->parent. Commented Aug 14, 2012 at 15:16
  • taking my words back...
    – Amit Kosti
    Commented Aug 14, 2012 at 15:24
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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