WordPress: If ($the_content) exists

Dale Crum |Doc4|July, 09, 2009

Developing custom themes for WordPress over long periods of time can easily bring to light plenty of issues that are only solvable through php coding. These are things that are not provided for within the WordPress Codex or through the incredibly simple use of their template tag system. As mentioned, it's not too hard to get back on track with the inclusion of a little additional php code or with the help of the many plugins available on the WordPress extend page.

The issue we are looking at is a site design incorporating the use of $the_content template tag with the possibility that $the_content may not actually be needed or even exist. In such a case, as a Page or Post may display content ( floated left ) next to images ( floated right ) and the opposite may be true, if no content exists we would end up with an odd white space where the content was meant to be.

The solution, create an if/else php statement so that: if $the_content does not exist then float the images across the full width and not just to one side. We scoured the WordPress Codex for an answer to this issue, thinking there must be a template tag for it somewhere. Unfortunately this isn't the case and so we wrote the following code.

The Code:

Example CSS Styling:

8 Comments, Questions & Answers

  1. Alan Commented On

    Why is it that just using $the_content in the "if" requirement part will not work?

    • Doc4 Commented On


      Because $the_content is a reserved tag specific to WordPress. Think of it this way, WordPress has pre-determined what should be happening when we use this tag and this not one of the ways which we are allowed to use it.

  2. Sasi Commented On

    There is a tag that returns the string for the_content()
    It's called: get_the_content()


    I myself do an if (empty($var)) check on it. That's all.

  3. Alan Commented On

    Ahhh, thank you very much. A little tip though, for front end developers like myself, it can become a huge burden dealing with code written inside out like that, with html inside of the php. I know a lot of programmers for whatever reason prefer to do it that way, but it seriously cripples a designers ability to shape the php generated raw data into an actual website.

  4. Bjarne Christensen Commented On

    Just wanted to say thanks… This piece of code is very useful.

    I edited it a bit;

    post_content ) { ?>

    • Bjarne,

      Glad I could help, can you reprint the changes you made for others to see? Thank you.

      Please be sure and wrap any code within the "code" tags for it to display properly in the comments.

  5. Martha Commented On

    heh. never mind. It's late and my brain is failing. Your code is 100% perfect. I don't know why I thought I had to change that line — it didn't make sense when I did it and then I didn't really believe it when it worked. And, sure enough, my "fix" was actually breaking it.

    In any case, thanks!!

  6. Martha Commented On

    Thank you! I've been searching for the easiest way to test for empty post content. Figured there would be some kind of built-in conditional tag, but as you pointed out, no such luck.

    I did have to make one change to your code though. In that first line I had to change the single equals sign to a double. Before that the statement wasn't working; after that I seem to be on my way to success.

  1. Hello HAL | Doc4 Advertising Agency