Use the built in current_post and post_count vars with php’s modulus operator to check where you are in the loop and output markup at the appropriate time.
if( $grid_loop->have_posts() ):
echo '<div class="row">';
while( $grid_loop->have_posts() ):
$grid_loop->the_post();
?>
<div class="col-xs-3 box">
<p><?php the_title(); ?></p>
</div>
<?php
// if this is the third post
// and not the last post
// close previous row and open a new one.
// note: current_post starts at zero, so we add 1 to it
if( ((($grid_loop->current_post + 1) % 3) == 0)
&& ($grid_loop->current_post + 1) != $grid_loop->post_count ):
echo '</div><div class="row">';
endif;
endwhile;
echo '</div>';
endif;
Related Posts:
- and tag not working
- Display posts side by side with custom query
- Is there a way to control both Order By and Order query parameters from one input field
- WP_Query: get 3 random posts from 10 latest
- Show posts without term
- Is there a way to extend WP_query so Custom Post Types can have properties?
- How to Compare Two Meta Fields
- paginate_links ignore my format
- $wp_query initiation?
- Changing Posts Per Page and offset with pre_get_posts
- Show two random posts from custom post type
- Add the “active” class only to the first loop item in a WordPress query [closed]
- Duplicate Queries
- Counting number of posts with Category B in Category A
- pagination doesn’t show up for custom post type
- How to modify query so it grabs only 90 posts in total?
- Get only 1 Most Recently Modified Child Post from Parent
- How to get a category in a list item class
- Trying to check and see if a post has a featured image outside of the main loop
- Pagination doesn’t work in custom page template [duplicate]
- wp_query a single custom post type?
- Should close $wpdb via $wpdb->close()
- Using pre_get_posts on a specific core/query block
- Efficient way to update multiple post meta
- Does meta_query need numeric values to be cast to integers?
- How to use Meta Value Compare in WP_Query For Max and Min numbers
- WordPress documentation – WP_Query arguments
- WP_Query with multiple meta fields filter?
- Using apply_filters(‘the_content’, $custom_query->post_content) alters output
- WP_Query Date Query After Minus Hour AND minus minutes
- Shortcode to show thumbnail and link for 3 random posts
- wp query template tags not working
- How to select events within current week using wp_query
- How to display upcoming events by dat with Modern Events Calendar Lite
- Wp_query Add specific pending posts
- How to Filter Posts by Custom Fields?
- Display posts from only one post form in custom query and exclude in main query
- Get pagination working for custom loops within page templates
- Sort query by author: 1 author, then others
- add_actions called from functions.php not returning good values
- How to orderby multiple meta fields if some fields are empty
- Order by meta value (numeric value lower to higher)
- Wp_query calculate points
- WP Query – Outputting JSON and normal loop of posts
- How to return a 404 when custom query matches nothing?
- Should wp_query automatic meta and term cache priming be used in an enviroment with an object caching
- wp_query with custom post type only returning last posted
- Display custom tags on pages that have a specific page parent
- Query posts with “non set” meta value
- Add filter post_where and passing post_type argument
- WP_Query infinite Loop
- Limiting number of related posts
- Ordering a mysql style datetime stamp with meta_query
- Advanced WP Query and/or
- 2 wordpress loops showing 1 post from same post type – how to avoid showing the same post?
- WP Query – order posts by meta field first and then order the rest
- Reusing content from front page on sub page
- Get Child Page IDs by Parent ID
- Wildcard search in WP Query
- Pagination works in custom query loop but it doesn’t work in shortcode
- Create a notification for post field
- date_query is showing duplicate results
- How to query posts for custom post type with featured image for first 2 post?
- Why is get_the_excerpt returning full content
- How to avoid filling up an array each time I run a WP_query?
- How to SQL query posts IDs by categories AND authors?
- Randomly pick one testimonial post
- Custom post type – filter content by custom meta box content
- Trying to output content from page, works locally but not online
- Admin: Custom Query Returning Permissions Error
- wp query remove posts from query then update max pages and posts found
- Combine relationship posts with existing wp_query
- Slick slider for post doesn’t display anything
- What is the equivalent WP_Query of a SQL Query?
- create custom shortcode wp and put php code in
- Count post meta entries
- use mysql variable in a $wpdb->query to reindex a column?
- How to get only present and past posts with post_date
- Query WP Posts, then list the taxonomies from those posts
- Query post with content only
- Search with WP_Query, but ignore href URLs in anchor tags?
- Displaying Pages in Nav Sub-Menu with Specific Taxonomy Terms?
- Is meta_key used for ordering, filtering, or both?
- compare for multiple meta values for same key in wp_query
- Taxonomy Terms That Don’t Exist Display Results
- Why is my query not giving results?
- Fetch only categorized posts
- Wrote a WP Cron Plugin and it triggers a fatal error upon activation
- Add pagination to table generated by wp_query
- Adding a Theme Customizer Control for Posts_Per_Page
- using next-page and previous page inside a wp_query
- Custom query from all posts & all child page from one parent page?
- wp_query conditionally get posts
- restrict posts per page
- the_excerpt() not working in custom archive
- WordPress extremely slow when using get_posts with multiple meta_query relations
- Refresh wp_query with no changes
- SQL query into to WP query
- Search results stuck on page 1
- How to choose between hooking into pre_get_posts or into parse_query