从类别到主页添加特定数量的帖子

问题描述 投票:0回答:1

请尝试从一个类别添加特定数量的帖子(5个帖子)到主页,但它不起作用!

我目前正在使用的代码没有显示5个帖子,而是显示该类别的25个帖子

<div id="content"> 
<ul class="disclosure table group"> 
<?php $catquery = new WP_Query( 'posts_per_page=5&cat=158' ); ?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li style="text-align: justify; font-weight: 500; color: #cc3366;">
<a href="<?php the_permalink(); ?>" style="color: #cc3366;" title="<?php the_title(); ?>">
<span style="font-size: 12px;"><?php the_title(); ?> </span>
</a> 
</li> 
<?php endwhile; 
wp_reset_postdata(); ?> 
</ul>
</div>
html wordpress
1个回答
0
投票

像这样编写代码,wp_query的cat属性是一个数组。所以代码应该像下面那样

$args = array(
        'post_type' => 'post',
        'cat' => array(158),
        'posts_per_page'=>5,
);
$catquery = new WP_Query($args); 
 while($catquery->have_posts()) : $catquery->the_post(); 

//your code goes here

 endwhile; 
 wp_reset_postdata();

尝试代码,然后让我知道结果。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.