帖子当前类别的输出标题

时间:2011-03-15 作者:Devise

如何输出每个特定帖子的当前类别标题?

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

2 个回复
SO网友:Devise

这是迄今为止我能找到的最简单的答案:

$类别=获取\\u类别();echo$类别[0]->cat\\U名称;

SO网友:Bainternet

假设每个帖子只有一个子类别,那么:

$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; ?>
只需更新“$parent\\u category\\u id=12;”具有组类别的真实ID。

结束