Support » Themes and Templates » if category description do this, else… a better way?

  • I’m working on a theme and on the category page I’m displaying the category description inside a styled div container, however when there’s no description available for a certain category this div looks pretty darn ugly all empty, I came up with a rudimentary solution but I’m not sure is the proper way, seems to work though.

    <?php if (category_description( $category ) == '') : ?>
    <?php else : ?>
    <div class="cat-descr">
    <?php endif; ?>
    <?php echo category_description( $category ); ?>

    any other idea to beautify the code? 🙂


Viewing 4 replies - 1 through 4 (of 4 total)
  • Use the Conditional Tags is_category() or ‘in_category()` as required in your situation. Also, The Loop article describes styling posts when belonging to different categories.

    mmm, I’m not sure I understand your answer, I mean I “know” about conditional tags and I use them to show posts from categories and so on but couldn’t find anything about category description or I missed it…

    how would i know and test if a category has a description or not?

    In my case (my php skills are somewhat limited) I just guessed that

    if category_description was equal to nothing (”) do this else do something else.

    The way I have it now it’s working but i’m not sure if is the right way to do it :-\

    So you saw that article, but did you see this example?

    is_category(‘Stinky Cheeses’)
    When the archive page for the Category with Name “Stinky Cheeses” is being displayed.



    Don’t think you read googlebots’s post properly Michel. He is not asking for a conditional dependent on category name but depending on whether a category description exists for the current category. Basically the way he is doing it is basing the conditional on the negative (if description is empty).

    Basically I stripped your code down to this googlebot:

    <?php if (category_description( $category ) == ”) : ?>

    <!– html for when there is no description –>

    <?php else : ?>

    <!– html for when there is a description –>
    <?php echo category_description( $category ); ?>
    <!– html for when there is a description –>

    <?php endif; ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘if category description do this, else… a better way?’ is closed to new replies.