While循环将在下一节中继续

时间:2020-01-23 作者:Pranavmtn

我希望while循环在第一节中显示三篇文章,在另一节中显示其余的文章。我对wordpress很陌生,对php没有太多实际知识。任何帮助都是非常可观的。

这是我的索引。php代码:

         <!-- first section starting -->
         <section class="main_section" id="masc">
         <div class="container cardbox_1">
         <div class="row cardbox_horizontal d-flex justify-content-center ">
          <!-- while looping-section starting -->
          <?php 
          $post_args = array( \'post_type\' => \'Student Form\', \'posts_per_page\' => 6 );
          $post_query = new WP_Query( $post_args ); 


         if ( $post_query->have_posts() ) : 
         while ( $post_query->have_posts() ) : $post_query->the_post(); ?>

         <?php if ( $post_query->the_posts() >= 3 ) :  ?>

          <!-- while loop -->
          <div class=" col-md-4 cards">
          <div class="card card_1">
          <h3 class="h3 card_head text-uppercase">
          <?php the_title(); ?>
          </h3>
          <div class="center_image d-flex justify-content-center">
          <a  rel="external" href="<php? the_permalink()?>">
          <img class="image_head" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">
           </a>
           </div>
          <div class="para_head"><?php the_content(); ?>  </div>
          <div class=" d-flex justify-content-end">
          <a href="#" class="btn btn-primary">VIEW MORE</a>
          </div>
          <div>
          </div>
           <!-- while loop-->

           <?php wp_reset_postdata(); ?>
           <?php endwhile; // ending while loop ?> 
           <?php else:  ?>
           <p><?php _e( \'Sorry, no Students matched your criteria.\' ); ?></p>
           <?php endif; // ending condition ?>
           <!-- while section ending -->

           </div>
           </div>
           </div>
           </section>
           <!-- while section ending -->


           <!-- middle section-(should not loop -this considered a border to top and bottom sections)ending -->
           <section id="babg">
           <div class="container">
           <div class="row ">
           <div class="col-md-12">
           <hr class="this bbg">
           </div>
           </div>
           </div>
           </section>
           <!-- middle section ending -->



           <!-- here the same code again (this is faulty-i need the below post as numbered 4 )-->
           <section class="main_section" id="masc">
           <div class="container cardbox_1">
           <div class="row cardbox_horizontal d-flex justify-content-center ">


           <?php 
           $post_args = array( \'post_type\' => \'Student Form\', \'posts_per_page\' => 3 );
           $post_query = new WP_Query( $post_args ); 

           if ( $post_query->have_posts() ) : 
           while ( $post_query->have_posts() ) : $post_query->the_post(); ?>


           <div class=" col-md-4 cards">
           <div class="card card_1">
           <h3 class="h3 card_head text-uppercase">
           <?php the_title(); ?>
           </h3>
           <div class="center_image d-flex justify-content-center">
           <a  rel="external" href="<php? the_permalink()?>">
           <img class="image_head" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">
           </a>
           </div>
           <div class="para_head"><?php the_content(); ?>  </div>
           <div class=" d-flex justify-content-end">
           <a href="#" class="btn btn-primary">VIEW MORE</a>
           </div>
           </div>
           </div>

           <?php wp_reset_postdata(); ?>
           <?php endwhile; // ending while loop ?> 
           <?php else:  ?>
           <p><?php _e( \'Sorry, no Students matched your criteria.\' ); ?></p>
           <?php endif; // ending condition ?>


           </div>
           </div>
           </section>


           <!-- pardon me for funny class names and my bad terminology -->

2 个回复
最合适的回答,由SO网友:Tim Elsass 整理而成

您需要的是PHPmodulo 运算符并根据需要调整标记:

<?php
    $i = 0;
    $did_hr = false;
    $post_args = array( \'post_type\' => \'Student Form\', \'posts_per_page\' => 6 );
    $post_query = new WP_Query( $post_args ); 


if ( $post_query->have_posts() ) : 

while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
    <?php if ( $i % 3 == 0 ) : ?>
    <section class="main_section" id="masc">
        <div class="container cardbox_1">
            <div class="row cardbox_horizontal d-flex justify-content-center">
    <?php endif; ?>
                <!-- while loop -->
                <div class=" col-md-4 cards">
                    <div class="card card_1">
                        <h3 class="h3 card_head text-uppercase">
                        <?php the_title(); ?>
                        </h3>
                        <div class="center_image d-flex justify-content-center">
                            <a  rel="external" href="<?php the_permalink()?>">
                                <img class="image_head" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">
                            </a>
                        </div>
                        <div class="para_head">
                            <?php the_content(); ?>
                        </div>
                        <div class=" d-flex justify-content-end">
                            <a href="#" class="btn btn-primary">VIEW MORE</a>
                        </div>
                    </div>
                </div>
    <?php $i++; if ( $i != 0 && $i % 3 == 0 ) : ?> 
            </div>
        </div>
    </section>
    <?php if ( ! $did_hr ) : $did_hr = true; ?>
    <!-- middle section-(should not loop -this considered a border to top and bottom sections)ending -->
    <section id="babg">
        <div class="container">
            <div class="row ">
                <div class="col-md-12">
                    <hr class="this bbg">
                </div>
            </div>
        </div>
    </section>
    <?php endif; ?>
           <!-- middle section ending -->
    <?php endif; ?><!-- while loop-->

<?php endwhile; // ending while loop ?> 
<?php else:  ?>
    <p><?php _e( \'Sorry, no Students matched your criteria.\' ); ?></p>
<?php endif; // ending condition ?>

SO网友:Nayan Chowdhury

如果要在列表中显示3篇文章,可以遵循以下模式:

<?php

$args  =  array(
        \'post_type\'         => \'post\',
        \'post_status\'       => \'publish\',
        \'orderby\'           => \'date\', 
        \'order\'             => \'DSC\',
        \'posts_per_page\'    => 3
        );
$post_query = new WP_Query($args);

if($post_query->have_posts()): while($post_query->have_posts()):$post_query->the_post();
?>
    <div>
        <h1> <?php the_title(); ?> </h1>
        <?php the_excerpt(); ?>
    </div>
<?php
endwhile;
endif;
?>
现在,如果要在另一个部分中显示其余的帖子,但要排除前3篇帖子,请使用偏移量。以下是示例:

<?php

$args  =  array(
        \'post_type\'         => \'post\',
        \'post_status\'       => \'publish\',
        \'orderby\'           => \'date\', 
        \'order\'             => \'DSC\',
        \'posts_per_page\'    => -1, // will show unlimited posts
        \'offset\'            => 3
        );
$post_query = new WP_Query($args);

if($post_query->have_posts()): while($post_query->have_posts()):$post_query->the_post();
?>
    <div>
        <h1> <?php the_title(); ?> </h1>
        <?php the_excerpt(); ?>
    </div>
<?php
endwhile;
endif;
?>
您需要根据需要编辑代码。希望这对你有帮助。

相关推荐