0

I am using get_the_content() to load other pages content on the current page, and I noticed some block CSS classes are missing in the HTML I get.

For example, is-layout-flex layout CSS class is missing for the Columns block, which is causing block to look different from how it looks when the page is open normally, in a browser tab.

When I open page normally, in a browser tab I get this:
<div class="is-layout-flex wp-container-7 wp-block-columns">

But, when I get the content of the page with the get_the_content(), I get this:
<div class="wp-block-columns">

Is there a way to make get_the_content() provide exactly the same content as it is provided when the page is open normally, in a browser tab?

4
  • Actually, why don't you just use the_content()? Why must get_the_content()? Because it will return the raw block HTML with the block comment delimiters like <!-- wp:columns -->.
    – Sally CJ
    Mar 15 at 12:29
  • Sorry for delayed answer Sally, Honestly I was not quite sure about the difference between them. I could use the_content() as well.
    – Tahi Reu
    Mar 16 at 12:26
  • 1
    In that case, then you should probably just use the_content(), unless if you don't want the filters like mentioned here (and many other filters) to be applied, which also means you'd need to manually apply the do_blocks() to ensure blocks are parsed properly (e.g. specific CSS classes are added).
    – Sally CJ
    Mar 16 at 12:40
  • 1
    It should also be noted that get_the_content() returns the content, whereas the_content() displays (i.e. echo) the content. But the_content() is not simply a shorthand/wrapper for echo get_the_content().
    – Sally CJ
    Mar 16 at 12:52

1 Answer 1

1

You can achieve that by applying the the_content filters, just like what the_content() does:

$content = get_the_content();
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]&gt;', $content );

echo $content;

Or the other way is by applying do_blocks() on the content:

$content = get_the_content();
$content = do_blocks( $content );

echo $content;
1
  • 1
    Thank you! do_blocks() is what I need.
    – Tahi Reu
    Mar 15 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.