【问题标题】:PHP loop not displaying postsPHP循环不显示帖子
【发布时间】:2021-05-05 19:30:39
【问题描述】:

背景: 我已经使用 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>

【问题讨论】:

  • 您是否验证了查询实际返回了多少帖子?
  • 嗨@ADyson,是的,浏览器呈现五个帖子显示。我在博客类别中有大约 13 篇文章。
  • 好的。但是您是否调试过代码并验证此特定查询 实际返回了多少帖子?例如您可以在循环开始时记录$i 的值,以查看它有多高。
  • 您的默认“每页帖子”设置是什么?因为您没有在查询中改变它。所以这可能是一个因素。 developer.wordpress.org/reference/classes/wp_query/…

标签: php html css wordpress loops


【解决方案1】:

您在 query(posts_per_page) 中缺少 1 个参数,因为它没有设置,查询使用的是您的 Settings->Reading 中的默认值:

$args = [
  'cat' => 8,
  'orderby' => 'date',
  'order'   => 'ASC',
  'posts_per_page' => -1
];

$portfolioPosts = new WP_Query($args);

【讨论】:

  • 谢谢@Angel Deykov! Wordpress 默认为每页 5 个帖子;作为参考,这是我使用的代码。 $portfolioPosts = new WP_Query('cat=8&amp;posts_per_page=13&amp;orderby=date');
猜你喜欢
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
相关资源
最近更新 更多