我想在子类别中显示子类别帖子,但不在父类别中显示

时间:2014-10-14 09:53:15

标签: php wordpress

<?php get_header(); ?>
    <section id="container" class="<?php echo tempera_get_layout_class(); ?>">
     <div id="content" role="main">
<?php if ( have_posts() ) : ?>

这是在带有图像的类别页面中显示子类别的代码

<?php $this_category = get_category($cat); ?>
        <ul style="line-height: 5.7em;">
         <?php  $id = get_query_var('cat');
         $args = array('parent' => $id );
         $catdesc = $cat->category_description;
          foreach (get_categories($args) as $cat):?>
              <a href="<?php echo get_category_link($cat->term_id); ?>">
              <img style= "width:250px; height:150px; padding-left:20px; margin-top:20px;" src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
               <div style="display:inline-block;position:absolute;margin-left:-150px;margin-top:150px;"><?php echo ("$cat->cat_name"); ?></div></a>
         <?php endforeach ?>            
         </ul>

Here the code ends for displaying sub category

<?php
$category_description = category_description();
if ( ! empty( $category_description ) )
echo apply_filters( 'category_archive_meta', '<div class="category-               archive-meta">' . $category_description . '</div>' );
?>
</header>
<?php while ( have_posts() ) : the_post(); ?>
    <?php
           get_template_part( 'content/content', get_post_format() );
        ?>
<?php endwhile; ?>
<?php if($tempera_pagination=="Enable") tempera_pagination(); else tempera_content_nav( 'nav-below' ); ?>
<?php else : ?>
    <article id="post-0" class="post no-results not-found">
        <header class="entry-header">
            <h1 class="entry-title"><?php _e( 'Nothing Found', 'tempera' ); ?></h1>
        </header><!-- .entry-header -->
        <div class="entry-content">
              <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps                          searching will help find a related post.', 'tempera' ); ?></p>
          <?php get_search_form(); ?>
        </div><!-- .entry-content -->
          </article><!-- #post-0 -->
       <?php endif; ?>
        <?php cryout_after_content_hook(); ?>
       </div><!-- #content -->
        <?php tempera_get_sidebar(); ?>
      </section><!-- #primary -->
<?php get_footer(); ?>

问题是该类别中的所有帖子都显示在类别和子类别下。

但我想只在该子类别下显示帖子,而不是在类别中。

所以请任何人给我建议

1 个答案:

答案 0 :(得分:0)

请使用以下代码。它会对你有所帮助。

$category = '{enter your category name here}';

$categoryID = get_cat_ID($category);

$subcategories = get_categories('child_of=' . $categoryID);

foreach($subcategories as $subcategory) {

   $subcategory_posts = get_posts('cat=' . $subcategory->cat_ID);

   foreach($subcategory_posts as $subcategory_post) {

           $postID = $subcat_post->ID;

           echo get_the_title($postID);
   }
}