I’d like to hook into the loop, ie. have_posts() maybe? and output some code before and after. Is there a filter for this so I can use it in a plugin without needing to edit themes?
2 comments
Comments are closed.
I’d like to hook into the loop, ie. have_posts() maybe? and output some code before and after. Is there a filter for this so I can use it in a plugin without needing to edit themes?
Comments are closed.
The function
the_post()
(source) triggers the actionloop_start
when it is first used in the loop. This is used in everyWP_Query
loop, so you may want to check if the current query is the ‘main’ one (assuming that’s the query you wish to target)The last time
have_posts()
is called it triggers the actionloop_end
(source), so similar to above:Thank @Stephen Harris for the above answer 🙂
Just a very important note, when using the loop start/end you need the code below to prevent unwanted trigger of your code in the head.