简体   繁体   English

仅在子类别下显示Wordpress帖子,而不在类别下显示

[英]show Wordpress posts under subcategory only, not category

I am using this code to display subcategory in a category 我正在使用此代码显示类别中的子类别

<?php $this_category = get_category($cat);

    $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); ?>">
<?php echo ("$cat->cat_name"); ?></a>

    <?php endforeach ?>     

    <?php while ( have_posts() ) : the_post(); ?>

    <?php
        get_template_part( 'content/content', get_post_format() );
    ?>

    <?php endwhile; ?>

The problem is that all the posts in the category display under the category and the subcategory. 问题是类别中的所有帖子都显示在类别和子类别下。

But i want to display posts under that subcategory only, not in category. 但是我只想在该子类别下显示帖子,而不是类别。

Try this:

$category = 'Jobs';

$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);
   }
}

For more details go to this https://wordpress.org/support/topic/how-to-list-posts-by-sub-category

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM