The global $wp_query is the default query you’re working on when you use have_posts()
, the_post()
etc, so just add
global $wp_query;
if($wp_query->current_post == 4) echo "4!";
global might or might not be necessary (and is certainly only necessary once, so just add it before the while loop), it depends whether you’re in the original template or in a template that has been included via a function call.
Related Posts:
- Inside a loop, how to change CSS class based on category name?
- Allow user to select categories that will display in post loop
- filter a loop base on specific category
- Problem with multiple loops in wordpress theme
- Show the number of the post
- How to get multiple loop in category.php, my scripts becomes madness
- Get the children of the parent category
- How to check if I’m on the last page of posts?
- How do I get the category URL from get_the_category?
- Check if current category has subcategories
- wp_nav_menu not appearing for a couple pages
- Default WP Gallery – show only galleries of a certain category
- wp_update_nav_menu_item() to insert categories
- Display all posts in category, with specific tag posts at top
- Loop with Dynamic Categories
- How can I create a loop to build slides based on multiple categories using Coda Slider
- Categories list loop – add separator every year
- How would you create a “weekly” archive?
- How to exclude categories from recent posts, recent comments & category widgets?
- Why adding Categories does not auto refresh in Backend while using my custom theme?
- Listing all posts from current category on page
- is_archive() content being shown instead of is_category()
- Listing Parent, Child and GrandChild Categories and then the PostTitles on Page Template !
- Category archive page – loop through posts of certain tag (with pagination) – pre_get_posts
- Retrieve all posts within tag OR category?
- Why would in_category only return one post in a specific category?
- New Theme creation
- How to get data about category in category.php
- editing fonts of category links from the_category() funtion
- Second level subcategory
- how to define category loop last class in the third
- Class active for Archive link – looped out
- Taxonomy name repeating when showing 2 posts from each category
- Exclude category from
- Different templates for different category tags
- Display the last post by each category?
- Why category.php throw 404 in wordpress while calling paginate_links()?
- Counter in loop in foreach
- Check if loop has any categories?
- Multiple Loops on category.php with pagination on last loop
- How to exclude the last post from a category(featured for example) from the main loop?
- Order by category titles
- ajax for filtering posts by category in wordpress loops
- Limit get_categories to show each category once
- Display single category name in woocommerce loop
- Question on using custom structures for categories
- Show Posts from Single Category Not Working
- Is there a reason I cannot get the current category in a loop?
- Category archive is displaying all posts rather than the specific category
- new WP_Query all post in a category inside the loop
- Problem with different query loops (and “main loop”) on category template page!
- How to add orderby parameter for sorting on category.php
- How to loop custom post_type from (a) specific category(ies)?
- How to display the parent category if only the child category has been chosen
- Category tags with comma’s
- Display posts of certain categories to specific user roles
- How do I retrieve the category ID (ugly permalinks) in my sub-navigation menu?
- Category nicename inside loop in wordpress
- Show post categories
- Add description to categories menu in admin
- multiple values in an array for category__and does not work with WP_Query
- List post only under the category, exclude child category content
- How to differentiate the homepage structure from the category page structure in WordPress template?
- Editing Loop So It Targets Specific Tags?
- How to detect /category and /tag base pages?
- How to get posts using category slug in ClassiPress?
- Posts made to category don’t show up (do show up on frontpage)
- List categories of a post hierarchically?
- Exclude parent categories from the_category() within the loop
- Exclude posts without category from loop
- Display all categories but only if they have posts in them that have a specific tag assigned
- In if…else condition, the else statement shows even if if statement is correct
- Please tell how to stop the category from showing up on posts
- Loop by category and meta_key
- Media Library Categories
- Assign a class to first element in category in loop
- Category page shows only 1 post when that post has more than 1 category?
- Configure query with multiple categories in a custom order?
- Show posts from all categories
- How can I modify this code to iterate over an array of categories?
- Loop within category’s posts
- How to exclude a specific categogy from a custom page template
- Display one post on category.php: Wrong featured image
- WordPress Numeric Pagination with Query String [duplicate]
- Loops in category description
- Category base 404 – fix
- Several loop in search result
- Printing direct descendants of a category with WP_Query
- altering theme – content generated by PAGES not PORTFOLIO
- My Query is getting the wrong data
- Detecting top parent category fails on home page
- How do i add custom post types to this query?
- Custom WordPress category page showing all posts rather than the specified category
- How do I hide posts in a category from all listings but still allow the posts to be viewed?
- How can i hide the authors box from a specific set of categories and post types?
- Do not show all post from all categories, just show posts from category in current loop
- How to display the category featured images [closed]
- Echo Category In Loop
- How can I implement pagination on custom category page?
- Get categories by title descendant