如何在Wordpress中显示最后三个自定义帖子类型
我很清楚这个问题已经被问了一百万次了,但我真的需要你的帮助,因为尽管我遵循了所有的建议,但我不能将自定义帖子类型的数量限制为3。这意味着,每次我创建一个新的自定义帖子类型(一个新的marathon),循环都会将其添加到其他帖子中。 我想要的是我的循环只显示最后3次马拉松。我认为这足以表明“每页帖子数”=>3,但事实并非如此 请帮忙!谢谢大家! 这是我的密码:如何在Wordpress中显示最后三个自定义帖子类型,wordpress,custom-post-type,Wordpress,Custom Post Type,我很清楚这个问题已经被问了一百万次了,但我真的需要你的帮助,因为尽管我遵循了所有的建议,但我不能将自定义帖子类型的数量限制为3。这意味着,每次我创建一个新的自定义帖子类型(一个新的marathon),循环都会将其添加到其他帖子中。 我想要的是我的循环只显示最后3次马拉松。我认为这足以表明“每页帖子数”=>3,但事实并非如此 请帮忙!谢谢大家! 这是我的密码: <?php $the_query = new WP_Query( 'post_type=kinsta_marathon' ); ar
<?php
$the_query = new WP_Query( 'post_type=kinsta_marathon' );
array(
'post_type' => 'kinsta_marathon',
'post_status' => 'publish',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'slider',
'field' => 'slug',
'terms' => 'slider'
)
)
);
// The Loop!
if ($queryObject->have_posts()) {
?>
<?php
while ($queryObject->have_posts()) {
$queryObject->the_post();
?>
<div class="container mb-3 py-3">
<div class="row h-100 pl-3 rowcalendartop ">
<div class="container h-100">
<div class="row h-100">
<div class="col-1 py-0"><img class="logomarathoncalendar" src="<?php the_field('logo_marathon'); ?>" alt="logo-marathon"></div>
<div class="col-10 py-0 align-self-center"><h5 class="mb-0 align self-center"> <?php the_title(); ?></h5></div>
</div>
</div>
</div>
<div class="row rowcalendarbottom h-100 greysection py-3">
<div class="col-2 align-self-center"><h6 class="mb-0"><i class="fas fa-map-marker-alt iconslidermarathon mr-3"></i> <?php the_field('where_marathon'); ?></h6></div>
<div class="col-2 align-self-center"><h6 class="mb-0"><i class="fas fa-calendar iconslidermarathon mr-3"></i> <?php the_field('when_marathon'); ?></h6></div>
<div class="col-2 align-self-center"><h6 class="mb-0"><i class="fas fa-running iconslidermarathon mr-3"></i> <?php the_field('km_marathon'); ?></h6></div>
<div class="col-2 align-self-center"><h6 class="mb-0"><i class="fas fa-euro-sign iconslidermarathon mr-3 "></i> <?php the_field('marathon_price'); ?></h6></div>
<a href="<?php the_permalink(); ?>" target="_blank"><div class="col-2 align-self-center"><h5 class="mb-0 text-center"><i class="fas fa-arrow-right iconslidermarathon mr-3"></i></h6></div></a>
</div>
</div>
<?php
}
?>
<?php
}
?>
<!--end loop-->
“alt=”logo marathon“>
您没有将参数传递给WP\u查询,只是传递post\u类型。将数组作为参数包含到WP\u查询中,它就会工作
$the_query = new WP_Query( array(
'post_type' => 'kinsta_marathon',
'post_status' => 'publish',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'slider',
'field' => 'slug',
'terms' => 'slider'
)
)
))
此外,如果循环是空的,则不需要使用if
包装循环。如果列表是空的,则不会处理循环,并且如果列表是空的,则不会显示任何类型的消息来指示没有记录
另一个更改是确保查询和循环的变量相同。您将查询变量命名为$the\u query
,但您正在通过$queryObject
进行循环
// The Loop!
<?php
while ($the_query->have_posts()) {
$the_query->the_post();
?>
//循环!
有关有用的示例,请参阅。非常感谢Russel,我遵循了您的指示,在清理了一点代码后,它工作得非常好!!您救了我!!!