Advertisement
salmancreation

Running Extra More Loops Offest WP Query

Dec 21st, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.76 KB | None | 0 0
  1. Running Extra Loops
  2.  
  3. It's important to be aware that while you can use WP_Query to run more than one loop, you have to reset the post data and start a second instance of WP_Query to do this. This is because each of your loops will be outputting data based on different arguments.
  4.  
  5. This example displays the excerpt and featured image for the first post and then just the title of each subsequent post:
  6.  
  7.    
  8. <?php
  9.  
  10. // First query arguments.
  11. $args1 = array(
  12.     'post_type' => 'post',
  13.     'posts_per_page' => '1'
  14. );
  15.  
  16. // First custom query.
  17. $query1 = new WP_Query( $args1 );
  18.  
  19. // Check that we have query results.
  20. if ( $query1->have_posts() ) {
  21.  
  22.     // Start looping over the query results.
  23.     while ( $query1->have_posts() ) {
  24.  
  25.         $query1->the_post();
  26.  
  27.         ?>
  28.  
  29.         <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  30.             <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
  31.                 <?php post_thumbnail( 'thumbnail' );?>
  32.             </a>
  33.             <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
  34.                 <?php the_title(); ?>
  35.             </a>
  36.             <?php the_excerpt(); ?>
  37.         </article>
  38.  
  39.         <?php
  40.  
  41.     }
  42.  
  43. }
  44.  
  45. // Restore original post data.
  46. wp_reset_postdata();
  47.  
  48. // Second query arguments.
  49. $args2 = array(
  50.     'offset' => '1',
  51.     'post_type' => 'post'
  52. );
  53.  
  54. // Second custom query.
  55. $query2 = new WP_Query( $args2 );
  56.  
  57. // Check that we have query results.
  58. if ( $query2->have_posts() ) {
  59.  
  60.     echo '<ul class="more-posts">';
  61.      
  62.         // Start looping over the query results.
  63.         while ( $query2->have_posts() ) {
  64.  
  65.             $query2->the_post();
  66.      
  67.             ?>
  68.  
  69.             <li <?php post_class(); ?>>
  70.                 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
  71.                     <?php the_title(); ?>
  72.                 </a>
  73.             </li>
  74.      
  75.             <?php
  76.      
  77.         }
  78.      
  79.     echo '</ul>';
  80.  
  81. }
  82.  
  83. // Restore original post data.
  84. wp_reset_postdata();
  85.  
  86. ?>
  87.  
  88. I've used two key arguments here:
  89.  
  90.     'posts_per_page' => '1', used with the first query, outputs just the most recent post.
  91.     'offset' = '1', used with the second query, skips the first post, ensuring it's not repeated in the list below.
  92.     As you can see from the code above, the loop is slightly different for each query. The first one outputs the featured image, title and excerpt, while the second checks if the query has posts and if so, opens a ul element and encloses each post title in a li element and a link to its page.
  93.  
  94. You'll also notice that I used wp_reset_postdata() after both loops. If I hadn't done this, the second loop would still output data from the first.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement