you can create a simple counter and condition:
<?php $count = 0; ?>
<?php if( $query->have_posts() ) : ?>
<ul class="posts">
<?php while ( $query->have_posts() ) : $query->the_post();
if ($count < 5){
?>
<li>
<?php the_title(); ?>
</li>
<?php
}else{
echo '</ul><ul class="posts">';
?>
<li>
<?php the_title(); ?>
</li>
<?php
$count = 0;
}
$count = $count + 1;
<?php endwhile; ?>
</ul>
<?php endif; ?>
Related Posts:
- Should we trust the post globals?
- WordPress Number of Posts Not Changing With posts_per_page
- Looping through posts per category gives same posts for each category
- Exclude current custom post on single post
- First x post with another template then the others
- WordPress loop not working on static front page
- Some doubts about how the loop work (trying debugging it)
- Get current taxonomy and display query accordingly
- Need to display a Jan 1st post as the site’s front page on Jan 1st, and Jan 2nd post as front page on Jan 2nd etc
- Display the latest post from some selected authors
- Advanced Post Display/Pagination/Ordering
- Why I obtain this WP_Query strange behavior trying to select posts having a specific tag?
- Grab next post title in archive page
- Query *only* sticky posts
- Can’t show comments count per post outside loop
- Alternate custom content in the loop
- wp_enqueue_media() slows down my WP site
- How can I get all posts data from within a paginated search result?
- Auto Refresh Post List after X seconds
- Display posts with comments closed, with pagination?
- posts_per_page doesnt work
- Sorting posts according to view counts not working
- Only display posts after current date
- Related posts loop based on tags AND categories doesn’t work without at least one tag assigned
- WP_Query Authors OR Categories
- Most liked page not displaying posts
- Query get post,how to add comment box
- WordPress Loop – Next 3 Posts
- Blog page pagination is not working after using the offset argument
- WP Query – Posts Per Page not working in combination with category__in
- Pulling in post category and children within category
- get query() without post content?
- How to add tags (custom taxonomy) to post class css?
- posts_per_page option limits the number of Gallery items
- How to exclude visited posts from loop
- Only display sticky post or latest post in custom loop
- get_the_category listing in hierarchial order
- why does wordpress ignore the post args?
- Why Query is returning empty array?
- Grid post page on WordPress with Bootstrap, the_excerpt(); Problem
- Loop parent terms {display posts} AND loop child terms {display posts}
- Wrap every month posts in div
- meta_value timestamp older than now
- Custom taxonomy archive page problem
- post__in not working with pre_get_posts, but post__not_in does work
- Loop don’t work within single.php page
- Query for first 3 posts to change the look and feel
- Query posts and filter at query time by value of custom meta
- Getting blog pagination to work on page set as front page
- Make loop inside slider divisible [closed]
- Very complex post query
- Delete post revisions only for a single post
- How to add automatically bootstrap 4 order-lg-1 and order-lg-2 classes for columns in foreach loop based on the count?
- Queries take 120+ seconds on my large WordPress site
- How to display post list in a table layout (multiple queries in single loop)
- Not all posts showing in query
- Optimize CPT-function with a loop
- How to display only one category in a custom post type?
- Limiting the amount of posts retrieved by the loop
- Display 3 posts with different HTML markup using a loop
- Changing wp_link_pages() to “Next Page” and “Previous Page” buttons?
- How to repeat a loop after 4 posts [duplicate]
- How to separate each individual blog post?
- Is there a way to define the $post var outside the loop?
- Only the most recent post is showing on my category page (working on localhost, not live site)
- Query to fetch custom taxonomy along with post title
- How to fetch courses in all languages in WordPress?
- How can I group by Taxonomy on Custom Post Type while filtering out based on custom Meta
- show loop id post , only first id can read
- Show 5 posts and than 3 posts offset with pagination
- I need some explanation on global $post [duplicate]
- Can I Paginate Post after every 25 tag?
- Alternative content between posts no repetition
- Start loop from specific post ID
- Display new posts categories in separated divs
- Querying posts from current category, using a variable as array argument
- Wp_query loop is not working as it should
- Query posts from newest category
- Problem with wp_insert_post()
- How to control Post view count incresing in sidebar widget posts too?
- WordPress loop uses unmodified posts array, why?
- Add row after three columns
- Modify WordPress loop after it has been run
- Skip posts, but keep posts per page
- Multiple Post Repeat for Related Post Loop by Category
- index.php is only displaying current month’s posts?
- Ordering posts alphabetically by meta keys and title
- Loop doesn’t display posts. It displays a link to the home page instead
- Why I have this strange behavior when I try to exlude the featured post from posts visualization?
- Where should get_post_meta() go to get $post->ID get_post_meta() is empty and $post->ID & get_the_id() are working
- Continuous listing from a custom field
- Getting posts from multiple users?
- Show post if in category
- Displaying categories items among posts
- How to store post rating system data post independent?
- How to limit post query to only return a total count of items with certain post statuses?
- Show all posts in category
- First post in loop displays twice
- Wrap every 2 posts
- What function does the loop of displaying posts?