The common mistake people make in this situation is they start to introduce bunch of custom counters to keep track of state.
Really WP_Query
just has those already. In your case that would be global $wp_query
and $wp_query->post_count
and $wp_query->current_post
. That should be quite enough to figure out even/odd overall and current iteration in the loop.
Related Posts:
- Query multiple custom post types in single loop
- Inject post (from specific category) between posts in Loop
- Build a content and excerpt grid loop with paging and options for # of posts
- Alter secondary loop to exclude posts from current page category
- How to get to a specific position in wp_query?
- have_posts() execution failure
- Using foreach loop breaks
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- WP_Query fails despite having 1 post
- wp_query get the 2nd post
- How to get post ID in a Page?
- How to add div blocks after certain set of post
- How to overwrite orderby with add_query_var
- get_the_ID() in the footer returns wrong value
- Get all posts as an array ID => Name
- WP_Query multiple post results
- Do not duplicate posts with multiple categories in multiple loops
- Infinite Loop – WP_Query
- Dividing the loop to style post differently
- How to extract information from a wp_query result?
- Search.php gets metadata from first post
- the_post(); prints out style text into my HTML?
- Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
- Wp Query : Order by distance lat,lon
- in_array not working on dev server but works on localhost
- How to display sticky post always at the top (before regular post) in wordpress?
- Making list of posts with chosen description
- My query keeps looping infinitely ! how to stop it?
- Efficient way of querying for a “fallback” post?
- For each 3 posts, show a different post type
- Add post class in custom loop (WP_QUERY)
- Create a hierarchical loop at predefined markup requirements
- Advanced Taxonomy query, with 3 post types
- Why does a meta_query break this WP_Query?
- WordPress post pagination on custom template not working
- Pagination in category.php not functioning
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- How do I exclude the current post from the upcoming post query
- How do I display WooCommerce products in my query to rows of 3?
- How to get posts by a certain author in inner loop using outer loop variable or post title WordPress
- Store loop into array
- Looping through WP_Post Object
- Display Post co-authored in Author Page
- Show Custom Taxonomy Title
- How to exclude specific category from the get_the_category(); array
- Sorting Posts | Alphabetical Order
- Looking for most performant way to execute several similar WP queries within shortcodes
- How to Display a Single Post Excerpt
- How to make sure content doesn’t display if selection is empty
- Changing layout with wp_customise
- How to use an associative array in post__in with WP_Query?
- Why my filterable portfolio page work not perfectly between slug button and slug output WORDRPESS?
- get author_name from queried post
- WordPress, AJAX and pre_get_posts using conditional tags
- Exclude posts based on meta value
- Remove echo from shortcode
- post thumbnail, conditions, else wont work
- unable to use ‘new WP_Query’ in AJAX call
- How to avoid duplicates when creating recent network posts
- variable value disappears in a second loop
- Trouble inputting variable into WP query
- WP_Query | IF within $args array | Help me only add specific arguments if the argument is not blank
- Why my loop isn’t working? [closed]
- How to get post ID from button and pass title to Modal
- Passing an array into WP_Query as a variable
- WordPress meta_query >= &
- Display child page content of a specific parent on home page
- How to wrap every 3 posts in a div (and close the last div too) [closed]
- WordPress loop is not working
- Displaying Event within two given time frames
- Remove Post if Advanced Custom Field is checked to fix paging
- Problem in replacing the_content with the_excerpt()
- Output product category link from WP_Query
- Pagination contents not changing in Front page WordPress
- Load more posts using AJAX based on posts inside WP_Query
- Custom search query on WordPress page not working
- How to select post ID for given parent name with nested prepared queries?
- Display all categories including sub categories under a list ul
- Adding new divs with custom classes to products on WooCommerce shop page [closed]
- Need to show 7 posts from actual date
- Add code to the header of posts by particular author
- Get Child of Child Pages in custom Menu
- Latest posts feed with a specific post always first
- Meta query not showing result properly
- Help on Wp_query to print an term
- How fix error in the WordPress loop?
- making a search.php query
- Loop 1 user randomly
- enumerating custom taxonomies?
- Query seems to be duplicated
- How to display thumbnail if post is assigned one otherwise not
- Pagenav Not appearing on custom Template
- Remove the_content From Loop
- Adding if statement to content for homepage
- Tables not showing divs and loop/php items
- Keeping the previous get value and add another value when submitted
- How to handle parent and child pages?
- Variable ++ in query loop
- a problem in class in class-wp-hook.php
- AJAX WP_Query’s order and orderby parameters not working