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:
|
1 2 3 4 5 6 7 8 |
<? 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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#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; } |
8 Comments, Questions & Answers
- Hello HAL | Doc4 Advertising Agency
Why is it that just using $the_content in the "if" requirement part will not work?
Alan,
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.
There is a tag that returns the string for the_content()
It's called: get_the_content()
—
http://wordpress.org/support/topic/if-content-exists
—
I myself do an if (empty($var)) check on it. That's all.
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.
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.
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!!
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.