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 have a problem checking if a post's category has a parent category, and if so retrieving the first post post of that category. Inside the the_post part:

This is my code:

    foreach((get_the_category()) as $childcat) {
            $parentcat = $childcat->category_parent;
            $parentcat_name = get_cat_name($parentcat);
    }

    echo "ID of parent category: ".$childcat->category_parent."<br />";
    if ($parentcat) {   // is there a parent? If so, this is a child post:

    $tmp_post = $post;  // assign orginal post to tmp_post variable for later use 

    //link name of parent category to first post in parent category
    global $post;
    $the_slug = 'my_slug';
    $args = array( 
        'category' => $parentcat, 
        'numberposts' => 1, 
        'order' => 'ASC' 
        ); 
    $my_posts = get_posts($args);
    if( $my_posts ) {
    $parentpost_link = $my_posts[0]->guid;
    }
        ?>  
        <h2 class='cat-parent'><a href="<?php echo $parentpost_link ?>"><?php echo $parentcat_name; ?></a></h2> 

The strange thing is that this works ok with one or two categories. While on another occation, when in a child category post, it fails to find that there is a parentcategory.

There is a similar problem with identifying a parentcategory's first childcategory, but I suppose the reason is similar to my problems with this one.

What I want is:

  1. To get the ID of the childcategory, either the one that the current post belongs to, or the only child category of this post. Then I make a slideshow consisting of all the posts in this category
  2. If the current post is a child-category, I need the ID of that category. The posts should not belong to more than max 2 categories: if the first found is the wrong one, I continue to find the next, which should be the correct one.
  3. If the post is in a child-category, I also want to find the permalink to the first post in the parentcategory, for navigation. This last task has also been a great difficulty. Strangely, this works perfectly with the first parent-child categories I set up, but not so with the other categories, although the database structure is correct.
share|improve this question
add comment

2 Answers

this is the very very simple code for showing all the parents categories ( include current category) of a post:

$category = get_the_category();
$current_category = $category[0];
echo get_category_parents($current_category, TRUE, ' &raquo; ');
share|improve this answer
add comment

The problem is in the logic. A post may have several terms, not all of which will be child terms. For instance, consider the parent > child term relationships:

a > b > c
d > e
f

And let's suppose that our post has the terms c,e and f attached to them.

Now the following loop goes through c,e and f in turn. But the $parentcat (and $parentcat_nam) value are over-ridden by each time:

 foreach((get_the_category()) as $childcat) {
            $parentcat = $childcat->category_parent;
            $parentcat_name = get_cat_name($parentcat);
    }

So the $parentcat will be set to the ID of the parent of the last term in get_the_category(). If that were c it would the ID of b. But if it were f, it would 0 and so 'fails to find a parent category'.

It's not clear what you want to do in the case of multiple terms with parents. If you want just one parent, then you can break out of the foreach loop.

 foreach((get_the_category()) as $childcat) {
            if( !empty($childcat->category_parent) ){
                $parentcat = $childcat->category_parent;
                $parentcat_name = get_cat_name($parentcat);
                break;
            }
    }
    //$parentcat is set the ID of the parent of one of the terms, 0 if all terms have no parent.

If you wanted to store the IDs of all parents of the post's terms, you can store them in an array

 $parentcat_arr=array();
 foreach((get_the_category()) as $childcat) {
            if( !empty($childcat->category_parent) ){
                $parentcat_arr[] = $childcat->category_parent;
                break;
            }
    }
  //$parentcat_arr stores array of IDs corresponding to the parents of the post's terms
  //If its empty, then the post has no terms with parents.
share|improve this answer
    
I am honestly not sure I understand all of your points here, but then I am not that experienced in these things. I've been misled to believe that category parent-child relations in wordpress are unique, and that other terms would not be mingled into it. Seems like your suggested break-solution works for finding the parents, but I still have problems finding the first post in the parent-category. –  user17818 Jul 4 '12 at 13:01
    
Each term has exactly one parent, yes. But if you have 2 terms, they do not necessarily have the same parent. In my example c and e both terms, but they have different parents. –  Stephen Harris Jul 4 '12 at 16:08
add comment

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.