[英]PHP loop not displaying posts
背景:我使用 PHP 構建了一個循環,以在我的 Wordpress 網站上創建我的投資組合帖子的動態提要。
問題:前五個帖子顯示得非常好; 但是,它后面的所有其他帖子都不是。 我不確定原因可能是什么,但任何幫助將不勝感激。 謝謝你。
<section id="portfolio">
<div class="container-fluid">
<div class="row justify-content-center mb-5 pb-5 animatedParent animateOnce" data-sequence="100">
<?php
$portfolioPosts = new WP_Query('cat=8&orderby=date');
if ($portfolioPosts->have_posts()) {
$i = 1;
while ($portfolioPosts->have_posts()) {
$portfolioPosts->the_post();
if ($i == 1){
?>
<div class="col-10">
<div class="row no-gutters">
<div class="col-12 col-md-12 col-lg-6">
<div class="row no-gutters">
<div class="col-12 animated fadeIn delay-300" data-id="1" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size:100% auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio3.png" class="img-fluid image-hover-portfolio"></a><!-- 12:4 -->
</div>
</div>
<div class="row no-gutters">
<div class="col-6">
<div class="row no-gutters">
<?php
} else if ($i == 2){
?>
<div class="col-12 animated fadeIn delay-300" data-id="2" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: auto 100%;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio2.png" class="img-fluid image-hover-portfolio"></a><!-- 4:12 -->
</div>
</div>
</div>
<div class="col-6">
<div class="row no-gutters">
<?php
} else if ($i == 3){
?>
<div class="col-12 animated fadeIn delay-300" data-id="3" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: 100% auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio1.png" class="img-fluid image-hover-portfolio"></a><!-- 4:3 -->
</div>
<?php
} else if ($i == 4){
?>
<div class="col-12 animated fadeIn delay-300" data-id="4" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: 100% auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio1.png" class="img-fluid image-hover-portfolio"></a><!-- 4:3 -->
</div>
<?php
} else if ($i == 5){
?>
<div class="col-12 animated fadeIn delay-300" data-id="5" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: 100% auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio1.png" class="img-fluid image-hover-portfolio"></a><!-- 4:3 -->
</div>
</div>
</div>
</div>
<div class="row no-gutters">
<?php
} else if ($i == 6){
?>
<div class="col-12 animated fadeIn delay-300" data-id="6" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: 100% auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio3.png" class="img-fluid image-hover-portfolio"></a><!-- 12:4 -->
</div>
</div>
</div>
<?php
} else if ($i == 7){
?>
<div class="col-12 col-md-12 col-lg-6">
<div class="row no-gutters">
<div class="col-6 animated fadeIn delay-300" data-id="7" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: 100% auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio1.png" class="img-fluid image-hover-portfolio"></a><!-- 4:3 -->
</div>
<?php
} else if ($i == 8){
?>
<div class="col-6 animated fadeIn delay-300" data-id="8" style="background-image:url('https://theartoflight.co/wp-content/uploads/2020/12/web_KOS3273.jpg');background-size: 100% auto;">
<a href=""><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio1.png" class="img-fluid image-hover-portfolio"></a><!-- 4:3 -->
</div>
</div>
<div class="row no-gutters">
<?php
} else if ($i == 9){
?>
<div class="col-12 animated fadeIn delay-300" data-id="9" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: auto auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio3.png" class="img-fluid image-hover-portfolio"></a><!-- 12:4 -->
</div>
</div>
<div class="row no-gutters">
<div class="col-6">
<div class="row no-gutters">
<?php
} else if ($i == 10){
?>
<div class="col-12 animated fadeIn delay-300" data-id="10" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: 100% auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio1.png" class="img-fluid image-hover-portfolio"></a><!-- 4:3 -->
</div>
<?php
} else if ($i == 11){
?>
<div class="col-12 animated fadeIn delay-300" data-id="11" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: 100% auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio1.png" class="img-fluid image-hover-portfolio"></a><!-- 4:3 -->
</div>
<?php
} else if ($i == 12){
?>
<div class="col-12 animated fadeIn delay-300" data-id="12" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: 100% auto;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio1.png" class="img-fluid image-hover-portfolio"></a><!-- 4:3 -->
</div>
</div>
</div>
<div class="col-6">
<div class="row no-gutters">
<?php
} else if ($i == 13){
?>
<div class="col-12 animated fadeIn delay-300" data-id="13" style="background-image:url('<?php the_post_thumbnail_url(); ?>');background-size: auto 100%;">
<a href="<?php the_permalink(); ?>"><img alt="" src="<?php echo get_template_directory_uri();?>/assets/images/blank-portfolio2.png" class="img-fluid image-hover-portfolio"></a><!-- 4:12 -->
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}else{
//do nothing
}
$i ++;
}
} else {
}
wp_reset_postdata();
?>
</div>
</div>
</section>
您在查詢(posts_per_page)中缺少 1 個參數,因為它沒有設置,查詢使用您的 Settings->Reading 中的默認值:
$args = [
'cat' => 8,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => -1
];
$portfolioPosts = new WP_Query($args);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.