The Easiest Way to Save and Share Code Snippets on the web

Add in dual loop into single loop

php-brief

last edit: Dec, 2nd 2011 | jump to bottom

  1. <?php
  2.  
  3. $wp_query = new WP_Query();
  4. $per_page=$paged<=1?7:8;
  5. $wp_query->query('posts_per_page='.$per_page.'&paged='.$paged);
  6.  
  7. if ($wp_query->have_posts()) :
  8.  
  9. while ($wp_query->have_posts()) : $wp_query->the_post();
  10.  
  11. // Check if paged
  12. if ($paged<=1) { ?>
  13.  
  14. <!-- LOOP NEWS POST -->
  15.  
  16. <div class="arc-mod" >
  17. <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" class="arc-mod-title">
  18. <?php the_title(); ?>
  19. </a>
  20. <div class="arc-mod-meta">
  21. <span>//</span> Posted By <a href="/blog/index.php?author=<?php the_author_ID(); ?>"><?php the_author_nickname(); ?></a> on <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_time('jS F Y') ?></a> | <span class="arc-mod-ilove"><?php if(function_exists(getILikeThis)) getILikeThis('get'); ?></span>
  22. </div>
  23. <div class="arc-mod-content">
  24. <div class="arc-mod-excerpt">
  25. <?php new_excerpt(500); ?>
  26. </div>
  27. <div class="arc-mod-center">
  28. <a href="<?php the_permalink() ?>" title="Real Full Article" class="arc-mod-img-link">
  29. <img src="<?php bloginfo('template_url'); ?>/images/photos/test.jpg" alt="<?php the_title(); ?>" />
  30. </a>
  31. </div>
  32. </div>
  33. </div>
  34.  
  35. <?php };
  36. break; // break the loop
  37. endwhile;
  38. ?>
  39.  
  40. <?php
  41. // Rewind the loop
  42. $wp_query->rewind_posts();
  43. // set the iterator
  44. $i=1;
  45. ?>
  46.  
  47. <div class="equal-height">
  48.  
  49. <?php
  50. // loop again
  51. while ($wp_query->have_posts()) : $wp_query->the_post();
  52. // check if it is even then skip
  53. $c = $i % 2;
  54. if ( $c === 0 ) continue;
  55. ?>
  56.  
  57. <!-- LOOP AFTER POSTS -->
  58.  
  59. <div class="midi-arc-mod" >
  60. <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
  61. <?php the_title(); ?>
  62. </a>
  63. <div class="midi-arc-mod-meta">
  64. Posted By <a href="/blog/index.php?author=<?php the_author_ID(); ?>"><?php the_author_nickname(); ?></a>
  65. </div>
  66. <div class="midi-arc-mod-content">
  67. <div class="midi-arc-mod-excerpt">
  68. <a href="<?php the_permalink() ?>">
  69. <img src="test.jpg" alt="<?php the_title(); ?>" />
  70. </a>
  71. <?php new_excerpt(500); ?>
  72. </div>
  73. </div>
  74. </div>
  75.  
  76. <?php $i++; endwhile; ?>
  77. <!-- END SPLIT LOOP 1 -->
  78.  
  79. <?php
  80.  
  81. // Rewind the loop
  82. $wp_query->rewind_posts();
  83.  
  84. // set the iterator
  85. $i=1;
  86.  
  87. ?>
  88.  
  89. <div class="equal-height"></div>
  90.  
  91. <!-- SPLIT LOOP 1 RETURNS 1, 3, 5 -->
  92.  
  93. <?php
  94.  
  95. // loop again
  96. while ($wp_query->have_posts()) : $wp_query->the_post();
  97.  
  98. // check if it is even then skip
  99. $c = $i % 2;
  100.  
  101. if ( $c === 0 ) continue;
  102.  
  103. ?>
  104.  
  105. <div class="midi-arc-mod" >
  106.  
  107. <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
  108. <?php the_title(); ?>
  109. </a>
  110.  
  111. <div class="midi-arc-mod-meta">
  112. Posted By <a href="/blog/index.php?author=<?php the_author_ID(); ?>"><?php the_author_nickname(); ?></a></span>
  113. </div>
  114.  
  115. <div class="midi-arc-mod-content">
  116. <div class="midi-arc-mod-excerpt">
  117. <a href="<?php the_permalink() ?>">
  118. <img src="test.jpg" alt="<?php the_title(); ?>" />
  119. </a>
  120. <?php new_excerpt(500); ?>
  121. </div>
  122. </div>
  123.  
  124. <div class="midi-arc-mod-foot">
  125. <a href="<?php the_permalink() ?>"><span>//</span> Read Full Article</a>
  126. </div>
  127.  
  128. </div>
  129.  
  130. <?php $i++; endwhile; ?>
  131.  
  132. <!-- END SPLIT LOOP 1 -->
  133.  
  134. <?php $wp_query->rewind_posts(); //Rewind the loop ?>
  135.  
  136. <!-- SPLIT LOOP 2 RETURNS 2, 4, 6 -->
  137.  
  138. <?php
  139.  
  140. // reset the iterator
  141.  
  142. $i = 1;
  143.  
  144. while ($wp_query->have_posts()) : $wp_query->the_post();
  145.  
  146. // check if it is odd then skip
  147.  
  148. if ( $i % 2 ) continue;
  149.  
  150. ?>
  151.  
  152. <div class="midi-arc-mod" >
  153.  
  154. <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
  155. <?php the_title(); ?>
  156. </a>
  157.  
  158. <div class="midi-arc-mod-meta">
  159. Posted By <a href="/blog/index.php?author=<?php the_author_ID(); ?>"><?php the_author_nickname(); ?></a></span>
  160. </div>
  161.  
  162. <div class="midi-arc-mod-content">
  163. <div class="midi-arc-mod-excerpt">
  164. <a href="<?php the_permalink() ?>">
  165. <img src="test.jpg" alt="<?php the_title(); ?>" />
  166. </a>
  167. <?php new_excerpt(500); ?>
  168. </div>
  169. </div>
  170.  
  171. <div class="midi-arc-mod-foot">
  172. <a href="<?php the_permalink() ?>"><span>//</span> Read Full Article</a>
  173. </div>
  174.  
  175. </div>
  176.  
  177. <?php $i++; endwhile; ?>
  178.  
  179. <!-- END SPLIT LOOP 2 -->
  180.  
  181. </div><!-- end equal height div -->
  182.  
  183. <?php $i++; endwhile; ?>
  184.  
  185. <div class="previous-link"><?php next_posts_link(__('<span>//</span> PREVIOUS STORIES')) ?></div>
  186. <div class="next-link"><?php previous_posts_link(__('<span>//</span> NEWER STORYS')) ?></div>
  187.  
  188. <?php endif; ?>
79 views