Insert DIV container below 1st search result

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.

Leave a Comment