Here’s one way of doing it:
<?php
$courses = get_posts( array(
'post_type' => 'courses',
'posts_per_page' => -1
) );
if ( $courses ) {
print "\n" . '<div style="background:pink">';
foreach ( $courses as $course_count => $post ) {
setup_postdata( $post );
the_title( "\n", '<br>' );
if ( 5 == $course_count ) {
print "\n" . '</div>';
print "\n" . '<div style="background:yellow;">';
}
}
print "\n" . '</div>';
}
wp_reset_postdata();
?>
Related Posts:
- How do I query by post format in WordPress 3.1
- query_posts sort in multiple directions
- Adding Variables to post query
- Static Front-Page Excerpts
- Placing post data (title, date, and excerpt) nested in static content
- When to use WordPress loop or Foreach loop?
- How do I query based on the modified date?
- WordPress loop problem: Multiple loops, index.php and is_paged causing duplicate posts on next page
- posts_per_page no limit
- Nested meta_query with multiple relation keys
- Alternative to query_posts for main loop? [duplicate]
- Query posts by custom taxonomy ID
- How to return results of a get_posts() in explicitly defined order
- Query posts: how to exclude results if post is in multiple categories
- query_posts ->using meta_compare / where meta value is smaller or greater or equals
- How to order posts by descending comment count on taxonomy page?
- Is it better practice to use query_posts, WP_Query, or get_posts to create various custom loops within a Page?
- WP 5.8 “Query Loop” block: where to place custom query?
- Sort X categories by last update and show image
- Pagination on archive.php page
- Best way to load page content in Fancybox popup?
- order post my meta value m/d/y format with year as included value
- _wp_page_template to dynamically use template
- Schedule Sticky Posts
- Total Count of Posts NOT in Selected Categories?
- WordPress Search Filter Only for Page with Child of Child of Child of Child of Child
- Select category in custom query
- Problem with single-page for my custom post
- How to modify the query to exclude posts by slug?
- How to make the list of post and load on same page with sub-pages [closed]
- How to get 4 Posts after the 5 most recent ones
- post_status => publish not working
- How to fetch all posts in a category that were published between two given dates?
- How do I make archives.php for one category only?
- get_meta_data within a loop
- making random query button using $_GET
- Is it possible to query_posts using post__in and then Loop through them in the ordered they were queried?
- How do I change the default sort order of a custom column on the posts page?
- how to exclude “featured” posts from the main loop?
- WordPress pagination showing same posts on each page
- Query posts by category AND custom field, then ORDERBY another custom field – help!
- pass query string on url to filter media
- Order posts by condition
- Co-authors plus problems with query_post
- Search breaks when querying main loop for category
- Authors List page – How to include only those with wp_user_level as Author
- How to select posts from multiple categories and metavalues?
- Adding conditional content to single post-type template
- Query posts by meta_key whose value is an array
- Show Blogroll of another WordPress site
- Why this code causes infinite loop?
- Each post is showing twice in my custom query…?
- query_posts() – problem with “s” parameter
- Page title in post query
- Question about querying posts
- Currently using a query_posts() in my theme – want to use a request filter in stead
- How to list most popular post
- Breadcrumbs stop working on a specific template file
- How can I query for all children posts regardless of parent
- INSERT in table row fatal error
- WordPress query reverse order
- How to reverse post navigation at bottom of post
- WP-API: get posts in multiple categories
- Order for custom field after the loop
- Multiple instances of query_posts
- Different layout based on post amount?
- If Query Post Returns 1 Post
- Keeping get_posts’ offset and is_paged() synchronized
- odd behaviours from template tags
- Having an issue doing a WP_Query with post_content and category__and
- How to get any type posts include attachments by ids?
- Modify main loop query for paged and meta key
- wordpress query_posts featured page always on top
- Create a Loop with Posts Ordered by Most Recent Comments
- get posts, run a custom query, and join the results
- Need to add a space after meta value in query_posts in wordpress for zipcode search
- Tax query get first product with attribute value in pre_get_posts
- Specifying image size used in template the_content
- allow user to edit posts made by others users based on the user role
- Ordering Posts By Meta Data
- No duplicate tags by category
- Order posts by ACF checkbox
- Post per page not working
- Show previous month’s posts
- query_posts result differs from wp_query request result
- If meta_key has value = 6 | Change the wp_posts.post_date to current date
- Query posts based on parents attribute
- Adding Category Argument into an Array for WordPress Query
- orderby not working for query_posts using array of IDs
- Collect posts from last 48 hours
- The_content display a different content from a previous loop
- How to go to tag archives using a form
- Run second query on page based on author of the first query
- Why doesn’t this page query work?
- Inserting two categories
- Resetting a Query using wp_reset_query() or wp_reset_postdata() does not work
- Sort posts with multiple meta_keys
- How to get custom post type posts with certain multiple tags names and category id?
- Create “archives by year and week” by category template
- Limitless amount of posts in custom archive page