I'd like to hook into the loop with a plugin, and output some code after a post index in the loop. Two example I can think of are:
- output code after the 1st post in the loop
- output code in the middle (after num posts / 2)
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.Sign up to join this community
Unfortunately there's no purposely provided hook available for this. The only hooks inside the loop are triggered at the start and the end of the hook.
the_post action, but this is probably far too generic to use (may cause undeseriable side effects) and is called a the wrong point for example:
The function used to iterate through the loop is
the_post() which calls
setup_postdata() to 'set up' the current post in the loop (sets up all the globals etc). This function triggers the action
... So this will be called at the top of the 'content' part of the post (typically - though not guaranteed - above the mark-up for the post).
Secondly, themes/plugins could cause
the_post to be called from within the loop for things other than the current post in the loop, While you can ensure your callback is only triggered in between the start & end of the loop, you can't be sure that all the times its triggered in between is when you intend your callback to run.