1

I want to start a div when loop starts in a single page ( also in the blog page ). And end that div when loop ends.

Obviously using any filter hook or action hook.

Thanks In Advance

1 Answer 1

3

As an example (adjust to suit your needs):

add_action('loop_start', function() {
   if ( is_single() || is_home() )
       echo '<div>';
});

add_action('loop_end', function() {
   if ( is_single() || is_home() )
       echo '</div>';
});

You may need to adjust the conditional statements further if you wish to target archive pages and the like — learn more about the template hierarchy here.

To understand these hooks further you should inspect the_post() and have_posts() methods within the WP_Query class in wp-includes/query.php.

But for your immediate pleasure...

loop_start hook:

/**
 * Fires once the loop is started.
 *
 * @since 2.0.0
 *
 * @param WP_Query &$this The WP_Query instance (passed by reference).
 */
do_action_ref_array( 'loop_start', array( &$this ) );

loop_end hook:

/**
 * Fires once the loop has ended.
 *
 * @since 2.0.0
 *
 * @param WP_Query &$this The WP_Query instance (passed by reference).
 */
do_action_ref_array( 'loop_end', array( &$this ) );
1
  • Viva la WordPress!
    – Adam
    Commented Aug 2, 2015 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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