我在主页上显示帖子类别。我能够明智地打印帖子类别,但我想在每个类别中将帖子限制为5如何做到:
这是我的代码:
$querystr = "SELECT * from wp_posts as wp INNER JOIN wp_term_relationships as wtr on wtr.object_id=wp.ID INNER JOIN wp_terms as wt on wtr.term_taxonomy_id=wt.term_id where wt.term_id IN (8,16,17) order by wt.term_id, wp.ID DESC
";
$pageposts = $wpdb->get_results($querystr);
$tem="";
if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); $b=$post->name;
if($tem !=$b){
$tem=$b;
?>
<h3 style="border-bottom:2px solid #3b8b98;" ><span style="background:#3b8b98;width:30%;"><?php echo $tem;?></span></h3>
<div class="post" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<i class="fas fa-hand-point-right"></i> <?php the_title(); ?></a>
</div>
<?php } else {?>
<div class="post" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<i class="fas fa-hand-point-right"></i> <?php the_title(); ?></a>
</div>
<?php
} endforeach; ?>
答案 0 :(得分:0)
您可以使用WP_Query wordpress函数来获得所需的结果。 https://codex.wordpress.org/Class_Reference/WP_Query
$query = new WP_Query( array( 'cat' => 4, 'posts_per_page' => 5 ) );
根据您的要求在上面的示例中添加参数。
答案 1 :(得分:-1)
将SQL查询改为:
SELECT * from wp_posts as wp INNER JOIN wp_term_relationships as wtr on wtr.object_id=wp.ID INNER JOIN wp_terms as wt on wtr.term_taxonomy_id=wt.term_id where wt.term_id IN (8,16,17) order by wt.term_id, wp.ID DESC LIMIT 0,5
我已将LIMIT 0,5
添加到最后,只检索了五个结果。