0

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)
1

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.

There is 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 the_post.

... 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.