我正在使用此代码在类别
中显示子类别<?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; ?>
问题是该类别中的所有帖子都显示在类别和子类别下。
但我想只在该子类别下显示帖子,而不是在类别中。
答案 0 :(得分:0)
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