I have created a custom page with fetches content from various posts using get_post(id).
I fetch the posts using this function:-
function get_my_post($id){
$get_post = get_post($id);
$content = apply_filters('the_content', $get_post->post_content);
echo $content;
}
However, the output seemed to be breaking the DOM. After some investigation, I realized the posts containing a gallery shortcode had a lone closing div at the end of the content, without a matching opening div. For example, the echoed output from one of the posts containing a gallery looks like this:-
<figure class='gallery-item'>
<div class='gallery-icon landscape'>
<img width="700" height="469" src="https://testsite.com/wp-content/uploads/DSC_0668-700x469.jpg" class="attachment-medium size-medium" alt="" srcset="https://testsite.com/wp-content/uploads/DSC_0668-700x469.jpg 700w, https://testsite.com/wp-content/uploads/DSC_0668-768x514.jpg 768w, https://testsite.com/wp-content/uploads/DSC_0668-1000x669.jpg 1000w, https://testsite.com/wp-content/uploads/DSC_0668-400x268.jpg 400w, https://testsite.com/wp-content/uploads/DSC_0668.jpg 1600w" sizes="(max-width: 700px) 100vw, 700px" />
</div>
</figure>
<figure class='gallery-item'>
<div class='gallery-icon landscape'>
<img width="700" height="469" src="https://testsite.com/wp-content/uploads/DSC_0251-700x469.jpg" class="attachment-medium size-medium" alt="" srcset="https://testsite.com/wp-content/uploads/DSC_0251-700x469.jpg 700w, https://testsite.com/wp-content/uploads/DSC_0251-768x514.jpg 768w, https://testsite.com/wp-content/uploads/DSC_0251-1000x669.jpg 1000w, https://testsite.com/wp-content/uploads/DSC_0251-400x268.jpg 400w, https://testsite.com/wp-content/uploads/DSC_0251.jpg 1600w" sizes="(max-width: 700px) 100vw, 700px" />
</div>
</figure>
<figure class='gallery-item'>
<div class='gallery-icon landscape'>
<img width="700" height="469" src="https://testsite.com/wp-content/uploads/DSC_0192-700x469.jpg" class="attachment-medium size-medium" alt="" srcset="https://testsite.com/wp-content/uploads/DSC_0192-700x469.jpg 700w, https://testsite.com/wp-content/uploads/DSC_0192-768x514.jpg 768w, https://testsite.com/wp-content/uploads/DSC_0192-1000x669.jpg 1000w, https://testsite.com/wp-content/uploads/DSC_0192-400x268.jpg 400w, https://testsite.com/wp-content/uploads/DSC_0192.jpg 1600w" sizes="(max-width: 700px) 100vw, 700px" />
</div>
</figure>
</div>
As you can see, there is an unmatched closing div at the end that shouldn't be there - which messes up the DOM.
Here is the content of the post that outputs the above:-
[gallery link="none" size="medium" columns="3" ids="79,78,75"]
If I remove the gallery shortcodes, all other types of content work fine. And if I echo the content raw without the_content filter, that works - although obviously this echos the gallery shortcode unrendered. But applying the filter to the gallery gives me this lone div.
Does anybody have any ideas as to what might be happening?
Thanks for your time