MrQwest

Empty WordPress Content

I’m just coming to the end of a WordPress build for a client where each post would be a collection of images, links, paragraphs of text and video. Various media types in play which, with the help of Advanced Custom Fields, made the build fairly easy.

However, certain posts also made use of the standard WordPress content field. To cater for these posts that did use the content field, I had to build in the code to support.

<article <?php post_class('block content') ?>>
    <?php the_content('<p>Read the rest of this entry »</p>'); ?>
</article>

Works a dream however on the posts that don’t use the standard content field, the page would still output my article tag complete with styles but no content. I ended up having empty spaces on my post pages. Eurgh.

But with the help of some conditional PHP code, I was able to fix this.

<?php if( $post->post_content != "" ) { ?>
    <article <?php post_class('block content') ?>>
        <?php the_content('<p>Read the rest of this entry »</p>'); ?>
    </article>
<?php } else {
    // show nothing
}?>

So here, the PHP IF statement checks to see if there’s any post_content linked with the specific $post. If it’s empty ( !="" ), it doesn’t show anything. If there’s something there, then it’ll show the article tags & content.

Lovely.


More in the Archive