获取帖子的子类别“名称”

时间:2012-03-11 作者:menardmam

我在购买模板中有这首颂歌。我想知道要添加什么来获取类别的子名称

mais显示类别为赞助商。但在赞助商类别中,有:金、银、铂。。。我想得到“颜色”类别,并将其输出为<div class="cat-name">platium</div>

代码如下:

<div id="carouselContainer">
<h2 id="sponsorsTitle"><?php echo get_cat_name( $carouselCategory ) ?></h2>
<ul id="carousel">
    <?php $showPostsInCategory = new WP_Query(); $showPostsInCategory->query(\'cat=\'. $carouselCategory .\'&showposts=\'. $carouselNumber .\'\');
    if ($showPostsInCategory->have_posts()) : while ($showPostsInCategory->have_posts()) : $showPostsInCategory->the_post();?>
        <li>
            <?php $data = get_post_meta( $post->ID, \'key\', true ); ?>
            <?php echo get_cat_name( $carouselCategory ) ?>
            <a href="<?php  if ($data[ \'custom_link\' ]) { echo $data[ \'custom_link\' ];} else { the_permalink(); } ?>">
                <?php the_post_thumbnail(\'sponsor\', array(\'title\' => "")); ?>
            </a>
        </li>
    <?php endwhile; endif; ?>
</ul><!--end carousel-->
</div><!--end carouselContainer-->
<?php } ?>

<div id="footerContainer">
<div id="footer"> 

2 个回复
最合适的回答,由SO网友:Zsono 整理而成

menardmam,这是我的解决方案:

    <?php $showPostsInCategory = new WP_Query(); $showPostsInCategory->query(\'cat=\'. $carouselCategory .\'&showposts=\'. $carouselNumber .\'\');
     if ($showPostsInCategory->have_posts()) :  while ($showPostsInCategory->have_posts()) : $showPostsInCategory->the_post(); ?>
      <li>
           <?php $data = get_post_meta( $post->ID, \'key\', true ); ?>
           <?php foreach((get_the_category()) as $category) {echo ( $category->cat_name != $carouselCategory ) ? $category->cat_name . \' \' : \'\';}?>
           <a href="<?php  if ($data[ \'custom_link\' ]) { echo $data[ \'custom_link\' ];} else { the_permalink(); } ?>">
           <?php the_post_thumbnail(\'sponsor\', array(\'title\' => "")); ?>
           </a>
      </li>
    <?php endwhile; endif; ?>
Theget_the_category 检索帖子的每个类别(父类别和子类别),但使用$category->cat_name != $carouselCategory 部分父类别将不显示,仅显示子类别。

SO网友:menardmam
<?php 
function get_me_the_child_cat($arr, $val){
    foreach ($arr as $key => $value){
    if ($arr[$key] == $val){
    unset($arr[$key]);
    }
    }
    $arr_clean = array_values($arr);
    return (get_cat_name( $arr_clean[0]));
}
?>

<div class="clear"></div>
</div><!--end content-->
</div><!--end contentContainer-->


<div id="carouselContainer">
<h2 id="sponsorsTitle"><?php echo get_cat_name( $carouselCategory ) ?></h2>
<div id="carousel">
<ul>
    <?php $showPostsInCategory = new WP_Query(); $showPostsInCategory->query(\'cat=\'. $carouselCategory .\'&showposts=\'. $carouselNumber .\'\');
    if ($showPostsInCategory->have_posts()) : while ($showPostsInCategory->have_posts()) : $showPostsInCategory->the_post();?>
        <li class="spacerli">
            <?php $data = get_post_meta( $post->ID, \'key\', true ); ?>
            <div class="headerlogo"><?php $all_cat = wp_get_post_categories($post->ID); echo (get_me_the_child_cat($all_cat,$carouselCategory));?></div>
            <a href="<?php  if ($data[ \'custom_link\' ]) { echo $data[ \'custom_link\' ];} else { the_permalink(); } ?>">
                <?php the_post_thumbnail(\'sponsor\', array(\'title\' => "")); ?>
            </a>
        </li>
    <?php endwhile; endif; ?>
</ul>
结束