Advertisement
kalponikoronno

dynamically added row break

May 20th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.88 KB | None | 0 0
  1. /*dynamically added row break*/
  2.  
  3. <?php $portfolio_count = $the_query->current-post + 1; ?>
  4. <?php if($portfolio_count % 4 = 0) : ?>
  5. </div><div class="row">
  6. <?php endif; ?>
  7.  
  8.  
  9. /*another*/
  10.  
  11. <?php $q = new WP_Query( array( 'posts_per_page' => 6 ) ); // Edit your query ?>
  12.  
  13. <?php if( $q->have_posts() ) : ?>
  14.  
  15.     <?php while ( $q->have_posts() ) : $q->the_post(); ?>
  16.  
  17.         <?php if( 0 === ( $q->current_post  )  % 3 ): ?>
  18.             <!--Begin Row:--> <div class="row" data-equalizer>
  19.         <?php endif; ?>
  20.  
  21.         <!--Item: -->
  22.         <div class="large-4 medium-4 columns" data-equalizer-watch>
  23.             <?php get_template_part( 'partials/loop', 'archive-grid' ); ?>
  24.         </div>
  25.  
  26.         <?php if( 0 === ( $q->current_post + 1 )  % 3  ||  ( $q->current_post + 1 ) ===  $q->post_count ): ?>
  27.             <!--End Row: --> </div>
  28.         <?php endif; ?>
  29.  
  30.     <?php endwhile; ?>
  31.     <?php wp_reset_postdata(); ?>
  32.  
  33. <?php else : ?>
  34.  
  35.     <?php get_template_part( 'partials/content', 'missing' ); ?>
  36.  
  37. <?php endif; ?>
  38.  
  39.  
  40. This should give you the following layouts with three items per row:
  41.  
  42. #posts: 1
  43. <div class="row" data-equalizer>
  44.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  45.         <--Content Stuff-->
  46.     </div>
  47. </div>
  48.  
  49. #posts: 2
  50. <div class="row" data-equalizer>
  51.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  52.         <--Content Stuff-->
  53.     </div>
  54.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  55.         <--Content Stuff-->
  56.     </div>
  57. </div>
  58.  
  59. #posts: 3
  60. <div class="row" data-equalizer>
  61.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  62.         <--Content Stuff-->
  63.     </div>
  64.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  65.         <--Content Stuff-->
  66.     </div>
  67.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  68.         <--Content Stuff-->
  69.     </div>
  70. </div>
  71.  
  72.  
  73.        
  74.  
  75. You can use the following built-in properties of the WP_Query() class:
  76.  
  77. WP_Query::current_post // Index of the current post, starts at 0
  78. WP_Query::found_posts  // The total number of posts found
  79. WP_Query::post_count   // The number of posts being displayed.
  80.  
  81. to make your life eaiser, instead of introducing custom counters.
  82.  
  83. Here's an example that works on my install:
  84.  
  85. <?php $q = new WP_Query( array( 'posts_per_page' => 6 ) ); // Edit your query ?>
  86.  
  87. <?php if( $q->have_posts() ) : ?>
  88.  
  89.     <?php while ( $q->have_posts() ) : $q->the_post(); ?>
  90.  
  91.         <?php if( 0 === ( $q->current_post  )  % 3 ): ?>
  92.             <!--Begin Row:--> <div class="row" data-equalizer>
  93.         <?php endif; ?>
  94.  
  95.         <!--Item: -->
  96.         <div class="large-4 medium-4 columns" data-equalizer-watch>
  97.             <?php get_template_part( 'partials/loop', 'archive-grid' ); ?>
  98.         </div>
  99.  
  100.         <?php if( 0 === ( $q->current_post + 1 )  % 3  ||  ( $q->current_post + 1 ) ===  $q->post_count ): ?>
  101.             <!--End Row: --> </div>
  102.         <?php endif; ?>
  103.  
  104.     <?php endwhile; ?>
  105.     <?php wp_reset_postdata(); ?>
  106.  
  107. <?php else : ?>
  108.  
  109.     <?php get_template_part( 'partials/content', 'missing' ); ?>
  110.  
  111. <?php endif; ?>
  112.  
  113. This should give you the following layouts with three items per row:
  114.  
  115. #posts: 1
  116. <div class="row" data-equalizer>
  117.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  118.         <--Content Stuff-->
  119.     </div>
  120. </div>
  121.  
  122. #posts: 2
  123. <div class="row" data-equalizer>
  124.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  125.         <--Content Stuff-->
  126.     </div>
  127.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  128.         <--Content Stuff-->
  129.     </div>
  130. </div>
  131.  
  132. #posts: 3
  133. <div class="row" data-equalizer>
  134.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  135.         <--Content Stuff-->
  136.     </div>
  137.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  138.         <--Content Stuff-->
  139.     </div>
  140.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  141.         <--Content Stuff-->
  142.     </div>
  143. </div>
  144.  
  145. and
  146.  
  147. #posts: 4
  148. <div class="row" data-equalizer>
  149.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  150.         <--Content Stuff-->
  151.     </div>
  152.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  153.         <--Content Stuff-->
  154.     </div>
  155.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  156.         <--Content Stuff-->
  157.     </div>
  158. </div>
  159. <div class="row" data-equalizer>
  160.     <div class="large-4 medium-4 columns" data-equalizer-watch>
  161.         <--Content Stuff-->
  162.     </div>
  163. </div>
  164.  
  165.  
  166.  
  167. /*another*/
  168.  
  169.  
  170. <?php /* Template Name: Page TOP movies */ get_header(); ?>
  171. <div class="container">
  172.  
  173.                 <div class="row">
  174.           <div class="col-lg-12">
  175.             <div class="page-header">
  176.               <h1 id="type">TOP filmai</h1>
  177.             </div>
  178.           </div>
  179.         </div>
  180.                
  181.                
  182.  <?php
  183.  
  184.   //Set the counter to 1
  185.   $i = 1;
  186.  
  187.   //Open the row div
  188.   echo '<div class="row"><div class="movie_list">'; ?>
  189.                
  190. <?php $custom_query = new WP_Query( array( 'posts_per_page' => 24, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'  ) );
  191. while($custom_query->have_posts()) : $custom_query->the_post(); ?>
  192.                        
  193.                                  <div class="col-md-2">
  194.                                         <div class="well">
  195.                                                 <h6><a href="<?php the_permalink();?>"><?php if (strlen($post->post_title) > 35) {
  196.                                                 echo substr(the_title($before = '', $after = '', FALSE), 0, 25) . '...'; } else {
  197.                                                 the_title();
  198.                                                 } ?></a></h6>
  199.                                                 <a href="<?php the_permalink();?>"><?php the_post_thumbnail('movie-thumb', array('class' => 'img-thumbnail')); ?></a>
  200.                                                 <a href="<?php the_permalink();?>"><button type="button" class="btn btn-default btn-sm btn-block"><span class="glyphicon glyphicon-play"></span> Žiūrėti</button></a>
  201.                                         </div>
  202.                                  </div>
  203.                                  <?php $i++; if($i % 6 == 0) {echo '</div></div><div class="row"><div class="movie_list">'; ?>
  204.                                  <?php }  ?>
  205.         <?php  endwhile;  ?>
  206.         </div></div>
  207. <?php wp_reset_postdata(); // reset the query ?>
  208.  
  209.  
  210. <?php get_footer(); ?>
  211.  
  212.  
  213. /*another*/
  214. <div class="row">
  215.         <?php
  216.         $args = array('post_type' => 'portfolio');
  217.         $the_query = new WP_Query($args);
  218.         ?>
  219.                 <?php if(have_posts()) : while( $the_query->have_posts()) : $the_query->the_post(); ?>
  220.                         <div class="col-sm-3 portfolio-piece">
  221.                                 <?php
  222.                                         $thumbnail_id = get_post_thumbnail_id();
  223.                                         $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true);
  224.                                 ?>            
  225.                                
  226.                                 <img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php the_title(); ?>" />
  227.                                 <h3><?php the_title(); ?></h3>
  228.                         </div>
  229.                        
  230.                         <!--dynamic row break code after 4 post-->
  231.                         <?php $portfolio_count = $the_query->current-post + 1; ?>
  232.                         <?php if($portfolio_count % 4 = 0) : ?>
  233.                                 </div><div class="row">
  234.                                 <!--every 4 posts close div and open new div with class </row>-->
  235.                         <?php endif; ?>
  236.                                                
  237.                 <?php endwhile; endif; ?>
  238. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement