I have the following function which wraps the text only from my wordpress post in row and content-wrap tags.

function sr_wrap_content_in_div( $content ) {
$contents = explode("<img", $content);

foreach($contents as $content) {
    $before_tag = strstr($content, '/>', true); 
    $after_tag = strstr($content, '/>');
    if( $before_tag == '' && $after_tag == '' ) {
        echo '<div class="content-wrap row"><div class="col-md-10 col-md-push-1">' . $content . '</div></div>'; 
    } else if( $after_tag == '/> ' ) {
        echo '<img' . $before_tag . '/>';
    } else {
        echo '<img' . $before_tag . '/>' . '<div class="content-wrap row"><div class="col-md-10 col-md-push-1">' . substr($after_tag, 2) . '</div></div>';
    }
}
}
add_filter( 'the_content', 'sr_wrap_content_in_div' );

However it creates empty divs in between images (when they are placed next to each other) and wraps them too. I’m not sure where these empty divs are comming from.

It works fine when it goes image then content (

or htags, etc) then another image. But when it goes image then another image it creates an empty div between them.

Read more here: Wrapping text content from WordPress post


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: