plugins - Output before and after the loop

I'd like to hook into the loop, ie. have_posts() maybe? and output some code before and after.Is there a filter f

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?

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?

Share Improve this question asked Jul 19, 2013 at 8:14 PoePoe 2414 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 8

The function the_post() (source) triggers the action loop_start when it is first used in the loop. This is used in every WP_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)

add_action( 'loop_start', 'wpse107113_loop_start' );
function wpse107113_loop_start( $query ){
     if( $query->is_main_query() ){

     }
}

The last time have_posts() is called it triggers the action loop_end (source), so similar to above:

add_action( 'loop_end', 'wpse107113_loop_end' );
function wpse107113_loop_end( $query ){
     if( $query->is_main_query() ){

     }
}

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.

if ( $query->is_main_query() ):

    ... code here

endif;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742307440a4419220.html

相关推荐

  • plugins - Output before and after the loop

    I'd like to hook into the loop, ie. have_posts() maybe? and output some code before and after.Is there a filter f

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信