WordPress: If ($the_content) exists

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:

<? if($content = $post->post_content ) {
      echo "<div id='content_wrap'>";
      the_content();
      echo "</div>";
      echo "<div id='image_right'>";
   } else {
      echo "<div id='image_fullWidth'>";
   } ?>

 

Example CSS Styling:

#content-wrap {
 width: 450px;
 margin: 10px 20px 10px 0;
 float: left;
 }

#image_right {
 width: 450px;
 margin: 10px 0;
 float: right;
 }

#image_fullWidth {
 width: 920px;
 margin: 10px 0;
 float: none;
 }
Dale Crum

Dale Crum

Owner / Creative Director at Doc4 Design