6

How can I insert a DIV just below the first search result (between result #1 and #2)?

  • Show us the search.php code and what do you want to include exactly? – Manoj Kumar May 20 '15 at 11:25
10

You should be able to do that with:

/**
 * Setup a custom hook before the second post on the search page
 */
add_action( 'the_post', function( $post, \WP_Query $q )
{
    if( $q->is_search() && $q->is_main_query() && 2 === $q->current_post )
    {
        do_action( 'wpse_before_second_post_in_search' );
    }
}, 10, 2 ); 

/**
 * Inject a Div after the first post on the search page
 */
add_action( 'wpse_before_second_post_in_search', function()
{
    echo '<div>My Injected Div</div>';
} );

where we've introduced the custom wpse_before_second_post_in_search hook, that you can now play with as you need.

  • Interesting approach ;-). Just one note, shouldn't you use the_posts filter instead of the_post, or am I missing something obvious here – Pieter Goosen May 20 '15 at 12:04
  • thanks, I'm always doing some typos ;-) here I actually I wanted to target the_post action, hehe I'm somewhat still sleeping .. @PieterGoosen – birgire May 20 '15 at 12:14
  • I was actually sleeping :-), slipped away for an hour to take a power nap. Have the flu and the medicine caught up with me, hahahaha – Pieter Goosen May 20 '15 at 12:21
  • Hope you get well soon, you could try the undocumented wp_delete_flu() ;-) @PieterGoosen – birgire May 20 '15 at 12:24
  • 2
    Hahahaha, excellent solution, if ( have_flu() ) { while ( have_flu() ) { the_flu(); /* Insert bugs and virusses here to be trashed */ } wp_delete_flu(); }. Enjoy – Pieter Goosen May 20 '15 at 12:29

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.