WP_Query
object contains FULL content of posts/pages retrieved from database. No HTML tags are being removed. Therefore, the following statement in your question:
It looks like WP_Query is parsing the HTML and then displaying what is
left.
is not true.
If you use, in your code, the_content()
instead of the_excerpt()
function, you’ll see that all original HTML tags are preserved.
It is important to understand how excerpts are being generated. What is being displayed by the_excerpt()
function? This is the code:
echo apply_filters( 'the_excerpt', get_the_excerpt() );
get_the_excerpt()
function is responsible for generation of the excerpt’s text.
From Codex:
Returns the excerpt of the post. This is either a user-supplied
excerpt, that is returned unchanged, or an automatically generated
word-counted trimmed-down version of the full post content.The auto-generated excerpts have all shortcodes and tags removed which
means it is just an unformatted string that would not see any
line-breaks in any form of output, since the actual line-breaks in the
raw text are also removed.
In your case, the_excerpt()
function displays an auto-generated excerpt, with HTML tags removed. To change this behavior, you can use ‘the_excerpt’ filter to add HTML formatting, or fill page’s Excerpt meta-box, with desired, HTML formatted text.