You can make use of the build in loop counter, $current_post
which you can access in the main query with $wp_query->current_post
. (Just a note, the counter starts at 0
, so post 1
will be 0
)
The main query uses WP_Query
, $wp_query
being the query variable used, so have a look at the Methods and Properties which you can make use of and the uses of these Methods and Properties
Inside your loop, just before endwhile
, add the following
if( 2 === $wp_query->current_post ) {
echo someFunction();
}
Related Posts:
- Show modified time if post is actually modified
- AJAX handler throws 400 (Bad request) – why?
- How to display the_post_thumbnail if a post has one or otherwise display the first image in a post?
- Most elegant way to enqueue scripts in function.php with foreach loop
- WordPress – thumbnail image from youtube (function and loop)
- Adding WooCommerce to a Custom Theme – not working [closed]
- A snippet after every image
- Function to show only first instance of shortcode
- Automatically insert php function into post $the_content
- Creating loop within functions.php
- Display WooCommerce subscriptions for user [closed]
- How to use max and min values of custom fields
- Custom Shortcode, functions PHP WP_Query loop
- Filtering posts by category name based on page’s slug
- 2 Loops, Only Displaying 1 Loop in Both Loops
- Having a Function Inside of the Loop
- Getting different functions data while using while loop in wordpress
- Create a new query in function.php to filter blog posts
- Limit length of first excerpt in the loop
- Default Custom Field Value Automatically Update
- Multiple Loops Meta Data
- Why doesn’t is_page(id) work in functions.php?
- The Loop not looping?
- How do I pull excerpts from pages?
- Output loop to function return?
- issue with if/elseif in_array inside foreach loop display only one post
- Genesis Framework: How to exclude a specific author from archive custom loop
- Problem with calling custom function in a foreach loop
- Get term slug by term id and then explode it
- Using functions.php to include code that’s processed inline
- Breaking up multiple words and inserting an image after first word
- Simplest Way to Build Custom Archives Page?
- Woocommerce Variable Product Dropdown for Custom Shop Template
- the_date() and the_time() functions display actual date an time instead of published date and time
- How set a while with a function
- Pass post ID from archive template to functions file
- Prevent function from triggering on current page
- How to use `foreach()` in ajax call
- Code executes outside of Loop while same code gives ‘Uninitialized string offset’ notice inside a while loop
- Only seems to be displaying one child when there are supposed to be multiple
- Integrate WP Tiles into existing loop (index.php) and theme
- Check if excerpt is empty at loop-portfolio
- Function to get the name in database table from the comma separated string
- Way of getting queried loop before the query with a filter hook?
- Post variables not displaying correctly in custom function
- Why don’t some template tags work when querying information on single.php?
- how to handle multiple forloop?
- Parse error: syntax error, unexpected ‘endforeach’ (T_ENDFOREACH) in [closed]
- How do grab the main loop, with conditions, and output via shortcodes
- Creating mixture of shortcodes to use in the visual/text editor
- Get full slug for a WordPress Post
- Update post meta with wp cron
- Upload Multiple Files With media_handle_upload
- What is the difference between get_page_link and get_permalink functions?
- Breadcrumbs showing Parent and Child Pages
- mysql custom wp query
- TinyMCE custom stylesheets for different post types
- Order get_users() by last login date. Is it possible?
- Display random text from a file with the WP built-in AJAX API
- How to call a function on particular page like ‘contact-us’ from function.php of child theme
- Detect Safari desktop browser and include the detection in a shortcode
- Is It wrong to use oop approach on functions.php?
- How to preserve edits to Name or Slug of term when using wp_update_term on save?
- Where do I add html code to the menu div?
- Add function to every post?
- Understanding and altering the structure of posted images
- What do the schemes login, login_post, admin, and relative mean?
- How do I hide or remove ‘Category’ from wordpress breadcrumbs
- Trouble with adding a wp_enqueue_script on wordpress
- Display images that are not in the content
- Display ‘BuddyPress Follow’ follow button to non-logged in users [closed]
- Pagination in relation to archive.php
- Incorrect redirect after commenting
- Function naming convention
- debugging errors.. how to remedy?
- Where do i create my own function in wp-admin
- In jquery use php variable to execute an enqueued jquery file
- Run a jquery script on on a certain template page
- Can’t add_action to ‘save_post’ and get it to fire
- How to load jQuery in TOP of wp_footer?
- How to fetch custom post type by variable date?
- How to make custom comment fields required
- How do I register a new settings page? [closed]
- Include Jquery libraries in wordpress theme?
- Retrieve First Image Function
- Display ACF category image on archive and single template files
- How to get taxonomy tree ids without running expensive loops
- Auto delete content in specific folder inside media library
- Using get_terms for custom taxonomy in functions.php
- How to update an image attachment’s alt text from a custom field when saving a post?
- Allow user only create specific tags
- Child theme remove parent filter in functions
- How to add publish/draft button in Admin Post Lists
- How to add number to wordpress count function?
- How do I integrate my Child Theme into a Custom Template?
- Sending Messages Back to the Template After Processing?
- Why are some custom javascript files working but some are not
- using enqueue_script in a shortcode isn’t working
- Filter to strip unnecessary attributes
- Get all users from role and add to dropdown (select) – wordpress, javascript