我有一个名为新闻的类别,其中有许多子类别。
我想要做的是从每个子类别(包括类别标题、帖子标题、附件图片)中仅获取 1 个帖子(最新)。
朋友们有什么建议吗??
<?php
$news_cat_ID = get_cat_ID( 'News' );
$news_cats = get_categories( "parent=$news_cat_ID" );
$news_query = new WP_Query;
foreach ( $news_cats as $news_cat ) :
$news_query->query( array(
'cat' => $news_cat->term_id,
'posts_per_page' => 1,
'no_found_rows' => true,
'ignore_sticky_posts' => true,
));
?>
<h2><?php echo esc_html( $news_cat->name ) ?></h2>
<?php while ( $news_query->have_posts() ) : $news_query->the_post() ?>
<div class="post">
<?php the_title() ?>
<!-- do whatever you else you want that you can do in a normal loop -->
</div>
<?php endwhile ?>
<?php endforeach ?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)