- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- The forum ‘Back End’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Hi guys, to jump straight in, I am trying to work with WP’s the_content() tag, expressed as a function.
The above expresses the_content as $content and outputs the text fine. What I want, however, is to check whether there is any actual text within the_content() and change the container div’s styling accordingly.
I have tried to do this with the following…
echo "
… but it has the effect of returning the content without any wrapping div (just
tags), and then opening and closing an empty div afterwards:
the_content text here
Any help is, as always, greatly appreciated.
P.S I tried doing this using a PHP string word count, but it would only return ‘0’ as the number of words…
Ahh. Here’s the answer:
http://digwp.com/2009/07/putting-the_content-into-a-php-variable/
The final code looks like this:
the_content();
$content = ob_get_clean(); ?>
echo "
This now lets me apply margins, padding, etc. to a container only if it will be actually filled with content. Otherwise, empty div! Thanks Chris!
You could also use the get_the_content()
function to return the content of the post to a variable instead of having to initialize a buffer.
<?php
$content = get_the_content();
if ($content != '') {
echo '<div>' . $content . '</div>';
}
?>
Just saw this now – nice one!