Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress: Check if the_content is empty / do something only when the_content is empty
<?php
$thecontent = get_the_content();
if(!empty($thecontent)) { ?>
// do or output something
<?php } ?> // break php tag for HTML block
@apermo

This comment has been minimized.

Copy link

apermo commented Sep 1, 2017

You could perfectly replace

$thecontent = get_the_content();
if(!empty($thecontent))

by

if ( !empty( get_the_content() ) )

And Thank you for the snippet :)

@ob-ivan

This comment has been minimized.

Copy link

ob-ivan commented Nov 17, 2017

@apermo, please bear in mind this syntax is only valid since PHP 5.5. WordPress is pretty conservative with back-compat, so if you produce a plugin that may be installed on older version of PHP, please consider using an additional variable like @bhongy did.

@stefanfisk

This comment has been minimized.

Copy link

stefanfisk commented Sep 21, 2018

the_content() also applies the the_content filter to the result of get_the_content(), so the method above might not actually work :/

@studiovanzwet

This comment has been minimized.

Copy link

studiovanzwet commented Nov 22, 2018

`

`
@netzgestaltung

This comment has been minimized.

Copy link

netzgestaltung commented Jul 11, 2019

just to summarize:

  $the_content = apply_filters('the_content', get_the_content());
  if ( !empty($the_content) ) {
    echo $the_content;
  }
@chunkybyte

This comment has been minimized.

Copy link

chunkybyte commented Sep 9, 2019

the_content() also applies the the_content filter to the result of get_the_content(), so the method above might not actually work :/

Is there a use-case where this should be a problem for an empty check? Just wondering if I still use the_content() for displaying the content and empty check using get_the_content(), shouldn't be an issue right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.