There are a few ways you could do it involving incrementing and checking a counter variable. I prefer to use the built-in member vars present in the main and every custom WP_Query
.
This will give you the index of the current post in the loop:
$my_query->current_post
Just remember that it’s zero indexed, meaning the first post is 0, second is 1, etc..
and this will give you the number of posts returned from your query:
$my_query->post_count
so we just combine those in an if
statement to check if it’s the last post in the loop. Remember to add 1 to current post here because it starts at zero:
while ($my_query->have_posts()) : $my_query->the_post();
$class="half";
if( ( $my_query->current_post + 1 ) == $my_query->post_count ){
$class .= ' last';
}
echo '<div class="' . $class . '">';
// rest of your markup, etc..
endwhile;
Related Posts:
- Query all posts where a meta key does not exist
- Single loop for wp_query and wp_user_query
- query multiple taxonomies
- Custom Post Type, WP_Query and ‘orderby’
- Include post id[s] into WP_Query()
- Syntax to get the Nth item in a list of custom post types?
- Grab 5 latest posts from custom post type ‘announcements’
- WP_Query to loop a Custom Field, Custom Post Types do not show
- Pagination Doesn’t Work
- Numeric pagination custom post type
- Using Query Posts With Multiple Post Types And A Taxonomy
- List with categories, subcategories and posts of custom posttype
- Sort the main query in subcategories/terms?
- How to show posts from multiple post types in a single loop? And display them separately on the same template
- Custom Taxonomy List links being re-written
- Can’t sort order of wp_query with 2 meta keys
- Want to filter only parent post in admin area
- Get count of custom post type created by current user
- Query Custom Post by Category
- Pagination Issue: Custom Post Type In Index
- Having trouble with custom date field for CPT query (WordPress)
- Quickest way to get last or oldest post date – WP Query
- Display posts if a custom field value is equal to another custom field value
- WP_Query and using a variable for ‘cat’=> in the args array = WP Bug?
- Custom post query by taxonomy
- Separate Posts and Custom Post Type in Custom Taxonomy archive template
- CPT posts listed by category with custom rewrite URL, please help!
- How to use WP_Query in a CPT achive page?
- Use get_post_types to query only custom posts types
- Group custom posts by custom taxonomy names
- How can i skip same post from taxonomy term?
- Using global $post; to get featured image for custom post via WP_Query
- Query custom post type by category slug
- Can’t query tag and post_type at the same time
- WP_Query orderby modified to include custom meta changes
- Wrong request query on cpt and tax
- Search / Filter posts on Title/Content OR Tags
- Converting the_content string to an array?
- A method for ordering mixed dates in search result loop (theory only, no actual code)
- Several post types on WP Query by tag and taxonomy
- Filter Posts from the Main Query
- Pagination with custom query, custom search form, single and pages, ajax and no plugins
- WP the_posts() on single-cars.php get category link
- tax_query not working in template
- Print current post category during WP_Query
- Multiple Custom Post Type queries, how to DRY it up
- Custom WP_Query always respond with 200 status even when no entry
- WPDB for post count on post
- post_per_page ignored in WP_Query
- Pagination 404 on Index with custom query
- WP_Query parameter conflict
- Retrieving meta-box from a custom-post-type
- How to include category name/id in wp_query for retrieving “custom post type” from a particular category?
- Cannot get custom post
- Custom post type and standard post not displaying or working
- Display custom post type for specific user
- Get all post from a post type
- Trying to add a class to post links
- A faster way to query custom post types with multiple conditions?
- WP_Query: Posts within category OR custom post type
- Two queries – one with checkbox ticked, one without – comparing meta_query
- posts_per_page in custom WP_Query does not override “Reading” settings?
- WP Query from two Custom Post type fields as statement
- Display Parent-Child Posts in specific order by comparing IDs in array
- Apply category query clause to posts of custom type
- Query to Exclude Child Pages from Custom Post Type Archive
- Randomizing WordPress Custom Post Type Sorting Through Them Without Page Refresh
- Order posts alphabetically with numbers but some of the posts has numbers in the title
- How to output custom post type title on custom page with category next to it?
- How to sort by multiple values in a nested WP_Query
- SEARCH QUERIES – REVERSE OUTPUT
- WP_Query to display number of custom post type filtered (order by) taxonomy
- 2 modal windows with ACF content on Archive page
- post_type incorrect for custom post type
- Live search by custom tag
- Custom Post Type – Display all, wrap in groups of 3
- How to get the posts that my following users are liked?
- Only show categories that have posts within custom post type
- SQL LIKE in WP_Query
- How can you use a page for a custom post type?
- Display all Custom taxonomy terms and their relevant custom posts
- CPT Efficient way to display posts from different categories with custom query
- How To Loop Through list with Custom Post Types
- CPT or Custom Table for Repository of Serial Numbers
- Query Pulling the same post twice
- Use Tags to Query Associated Multiple Posts and Get The Average Of Custom Field Values
- Custom Post Type + Category archive
- Values inside a custom field to determine which category posts to display
- WP_Query get posts where post_name is empty
- How do I sort post listing by child post count?
- Pop up showing same content on all posts display.
- Custom post-type’s pagination not working in category.php
- Listing all custom post types using a specific term on the said term’s template page, in groups
- Count of posts with meta_key filled in?
- Archive page – problem with pagination
- Are custom posts included when getting a categories’ posts?
- Query order by meta value force specific tag first
- Quering array of post types & pagination. Articles are repeating sometimes on different pages
- How can I show posts with the same tag?
- Load posts via AJAX without draft status