繁体   English   中英

PHP 循环不显示帖子

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM