WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show/Hide Category Descriptions (7 posts)

  1. stablegirl
    Member
    Posted 6 years ago #

    Hi,

    I'm designing a new theme for a friend's blog, and I have it set up so that category descriptions are displayed at the top of each category page. I'm using this code:

    <p><?php echo category_description(); ?></p>

    And the style sheet formats the paragraph so it has a gray background and a border. It works great.

    Here's the problem: Not every category has a description, and when there's not a description, the page displays an empty, ugly gray box.

    Can someone help me with an IF statement that would check to see if there's a category description, and if there is, display it in the gray box; if there's not, don't display anything?

    I'm thinking it has something to do with checking to see if the strlen variable is 0, but I'm not familiar enough with PHP to get the syntax right.

    Any help would be much appreciated!!

  2. iridiax
    Member
    Posted 6 years ago #

    I never figured this out myself, so I used the is_category() conditional tag to not use <?php echo category_description(); ?> for specific categories that I knew didn't have a description.

    http://codex.wordpress.org/Conditional_Tags#A_Category_Page

    Since <?php echo category_description(); ?> automatically wraps itself in paragraph tags, you don't need to put them around it. Categories without descriptions won't get empty paragraphs if you just use this:

    <?php echo category_description(); ?>

  3. stablegirl
    Member
    Posted 6 years ago #

    Thanks for replying, iridiax!

    In this case, I was dealing with several dozen categories, so I needed a way to do it automatically. After much trial and error, this code did the trick:

    <?php if (strlen(category_description()) > 0) {
    echo "<div id='catdesc'>";
    echo category_description();
    echo "</div>";
    }
    ?>`

    Turns out the styling has to be applied inside the IF statement.

  4. clicksharpmarketing
    Member
    Posted 5 years ago #

    This looks like you guys resolved the matter... just one question, where exactly did you make that edit? The theme I am using doesn't seem to show category description by default, so I have no placeholder.

    Thanks.

  5. fvxlpu
    Member
    Posted 5 years ago #

    clicksharpmarketing, I have the same situation. Go into the directory of you theme, open archive.php and look for a line like the following:

    <h3 class="pagetitle">Archives for the ‘<?php echo single_cat_title(); ?>’ Category</h3>

    Below this line, you can put the code discussed above, and your category descriptions will show up below the text "Archives for the ‘mycategory’ Category"

  6. fvxlpu
    Member
    Posted 5 years ago #

    The if-code from above, I replace with this if-statement:

    if (strlen(trim(category_description())) > 0 && trim(category_description()) != "<br />")
    ...

    Notice the html-br. In my instalation, when my category descriptions are empty, they output a string including a html-br, so I make sure to avoid the br

  7. clicksharpmarketing
    Member
    Posted 5 years ago #

    Nailed it. Thanks everyone for your help.

Topic Closed

This topic has been closed to new replies.

About this Topic