简体   繁体   中英

PHP loop not displaying posts

Context: I've built a loop using PHP to create a dynamic feed of my portfolio posts on my Wordpress site.

Problem: The first five posts are displaying perfectly fine; however, all the other posts following it aren't. I'm unsure what the cause could be but any help would be greatly appreciated. Thank you.

<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>

You're missing 1 argument in your query (posts_per_page), since it's not set, query is using default value from your Settings->Reading:

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

$portfolioPosts = new WP_Query($args);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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