I am attempting to add a conditional function to my index.php that will return the description of a term in my custom taxonomy on the page for that term above the list of posts only if the description exists. I want it to be in a styled div (id="term-descr"). So I tried:

<div id="term-desc">
<?php echo term_description() ?>

It worked fine on terms I had the description for, but on all other pages I ended up with the styled outline of the div with nothing in it, which looks funny. I think the function returns a br or something when it returns no value. So I tried to come up with a conditional function to make this work, something like:

<?php if (term_description() != 0 ) echo {?>
<div id="term-desc">
<?php term_description(); ?>
<?php } ?>

which doesn't work. I'm wondering if there is an easier way to do this or if I'm missing something.


term_description() returns a string, which might be empty.

So the test should be:

$term_desc = term_description();
# debug
print '<pre>' . htmlspecialchars( print_r( $term_desc, TRUE ), ENT_QUOTES, 'utf-8', FALSE ) . '</pre>';

if ( '' !== $term_desc )
    echo "<div id='term-desc'>$term_desc</div>";
  • thanks for the response. This worked (I added a ; after the first line) for terms with descriptions, but on pages with out a description I see the outline of the 'term-desc' div. When I look at the source I don't see anything inside the div, but it still gets opened and closed.
    – JPollock
    Jan 2 '13 at 0:29
  • 1
    @JPollock Inspect var_export( $term_desc ); to see what you get here. There is something wrong, maybe a plugin is filtering the value.
    – fuxia
    Jan 2 '13 at 0:34
  • Pardon my ignorance, but how do I inspect a variable? I tried this with all plugins disabled and nothing changed.
    – JPollock
    Jan 2 '13 at 1:13
  • 1
    @JPollock I have added a debug line to my example.
    – fuxia
    Jan 2 '13 at 5:53
  • This works perfectly. Weird thing is that it is printing the debug output in the middle of the page above the div that houses the description, which is working fine. I have debugging mode turned off in wp-config.
    – JPollock
    Jan 2 '13 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.