Advertisement
alchymyth

loop break wpse 359288

Feb 25th, 2020
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <!-- CUSTOM SECTION START -->
  2.  
  3.  
  4. <div class="container">
  5.   <div class="row">
  6.   <?php
  7.  
  8.    $posts_per_first_section = 5; //how many posts you want to show in the first section//
  9.  
  10.   $args = array(
  11.   'post_type' => 'post',
  12.   'posts_per_page' => 11,
  13.    );
  14.  
  15.    $blogposts = new WP_Query($args);
  16.  
  17.    while($blogposts->have_posts()) {
  18.    $blogposts->the_post();
  19.  
  20.    if ($blogposts->current_post < 2) :
  21.    ?>
  22.    <div class="col-md-6">
  23.    <?php else : ?>
  24.    <div class="col-md-4">
  25.    <?php endif; ?>
  26.   <a href="<?php the_permalink(); ?>">
  27.     <div class="card border-0">
  28.       <div class="card-picture">
  29.  
  30.         <img class="card-img" src="<?php echo
  31.       get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Card image">
  32.  
  33.         <div class="card-img-overlay d-flex flex-column">
  34.           <h5 class="card-title font-weight-bold"><?php the_title(); ?></h5>
  35.           <div class="mt-auto"><?php the_author(); ?> - <i class="fas fa-
  36.       clock"></i> - <?php the_time('d/m/Y')?></div>
  37.         </div>
  38.       </div>
  39.     </div>
  40.      </a>
  41.    </div>
  42.  
  43.     <?php
  44.     //check for 'break' out of first loop//
  45.         if( $blogposts->current_post+1 == $posts_per_first_section ) break;
  46.  
  47.     } //end of the first 'while' loop//
  48.  
  49.    ?>
  50.    
  51.    </div>         <!-- closing html for first loop -->
  52.    </div>        <!-- closing html for first loop -->
  53.  
  54.  <?php //checking if there are more posts to show in second loop
  55.         if( $blogposts->current_post > 0 && $blogposts->current_post+1 < $blogposts->found_posts ) : ?>
  56.  
  57.         <!-- opening html for second loop -->
  58.            
  59.     <div class="container">
  60.       <div class="row">
  61.  
  62.         <div class="col-md-8">
  63.        
  64.     <?php
  65.     while($blogposts->have_posts()) {
  66.     $blogposts->the_post();
  67.  
  68.      ?>
  69.           <div class="card-3">
  70.             <div class="row no-gutters">
  71.               <div class="col-md-5">
  72.                 <a href="<?php the_permalink(); ?>">
  73.                 <img class="card-3-img card-img-top h-100" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="...">
  74.                 </a>
  75.               </div>
  76.               <div class="col-md-7">
  77.                 <div class="card-body">
  78.                 <a href="<?php the_permalink(); ?>">
  79.                 <h5 class="card-title-3"><?php the_title(); ?></h5>
  80.                 </a>
  81.                   <span class="card-text"><?php the_excerpt(); ?></span>
  82.                   <div class="mt-auto"><?php the_author(); ?> - <i class="fas
  83.  fa-clock"></i> - <?php the_time('d/m/Y')?></div>
  84.                 </div>
  85.               </div>
  86.             </div>
  87.           </div>
  88.  
  89.           <?php } ?>
  90.             </div>   <!-- closing html for second loop -->
  91.             </div>   <!-- closing html for second loop -->
  92.             </div>    <!-- closing html for second loop -->
  93.            
  94.         <?php endif; //end checking if there are more posts to show in second loop
  95.          ?>
  96.  
  97. <!-- CUSTOM SECTIION END -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement