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.

How do I output the current category title for each specific post?

            <?php query_posts('category_name=group&posts_per_page=10'); ?>
            <?php if (have_posts()) : $counter = 0; ?>
                   <?php while (have_posts()) : the_post(); ?>    

                <div <?php if($wp_query->current_post % 2 == 0) post_class('clearfix'); else post_class('clearfix last'); ?>>
                    <?php the_post_thumbnail('thumbnail'); ?>

                    <span class="title"><em>CHILD CATEGORY TITLE HERE</em></span>

                    <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                    <?php the_excerpt(); ?>
                </div>

                   <?php $counter++; endwhile; ?>
            <?php endif; ?>
share|improve this question

2 Answers 2

This is the simplest answer I could find so far:

$category = get_the_category(); echo $category[0]-> cat_name;

share|improve this answer
1  
If this is what you are looking for then your question title is really misleading. its just the "category associated with that post" and it has nothing to do with child categories. child categories means that you have a parent category. Anyway glad you got what you need –  Bainternet Mar 15 '11 at 20:26
    
Maybe my title was a bit misleading - My bad. It should have said "If a post is a child category - how can I output the child category title"? –  Devise Mar 15 '11 at 22:09
    
Again, posts don't have child categories, only categories associated. only a category can have a child category just like Pages can have child pages, but posts will never have child categories. –  Bainternet Mar 16 '11 at 0:19

Assuming that each post has only one child category then:

$parent_category_id = 12;
 <?php query_posts('category_name=group&posts_per_page=10'); ?>
 <?php if (have_posts()) : $counter = 0; ?>
    <?php while (have_posts()) : the_post(); ?>    
        <div <?php if($wp_query->current_post % 2 == 0) post_class('clearfix'); else post_class('clearfix last'); ?>>
            <?php the_post_thumbnail('thumbnail'); ?>
            <?php
            $post_categories = wp_get_post_categories( $post_id );
            $cats = '';

            foreach($post_categories as $c){
                $cat = get_category($c);
                if ($cat->category_parent = $parent_category_id){
                    $cats = $cat->name;
                }
            }?>
            <span class="title"><em><?php echo $cats; ?></em></span>
            <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
        </div>
    <?php $counter++; endwhile; ?>
<?php endif; ?>

and just update "$parent_category_id = 12;" with the real ID of group category.

share|improve this answer
    
Would that output a single category name or multiple child category names (if there were multiple child cats associated)? Thanks! –  Devise Mar 15 '11 at 19:32
    
only one , and it must be a child of what ever you define at $parent_category_id = 12; –  Bainternet Mar 15 '11 at 20:01
    
There should be an easier way to say "Show this post's child category title" .. when each post will only be assigned one child category. No? –  Devise Mar 15 '11 at 20:16

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.