son*_*ill 5 wordpress categories posts
我有一个名为news的类别,里面有很多子类别.我想做的是从每个子类别(包括类别标题,帖子标题,附件图像)中只获得1个帖子(最新).朋友有什么建议吗?
The*_*dic 12
<?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 ?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7947 次 |
| 最近记录: |