• Resolved ralph23

    (@ralph23)


    Alrighty.

    I know how to display categories with php code in the wordpress theme files. What I’m trying to do is display all the categories in the sidebar, and under each category, I want to display the description of the category.

    Yet after searching a bit, I can’t find a good, solid, definitive answer.

    Any help appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You can make a function and add it to your themes functions.php file like so:

    /* descriptive cat list by tugbucket.net :D */
    function desc_cats(){
    foreach (get_categories(array('hide_empty'=>true)) as $category)
    {
    $catid = $category->cat_ID;
    echo '<li><a href="' . get_bloginfo('wpurl') . '/category/' .  $category->category_nicename . '/">' .
    	$category->cat_name . ' (' . $category->count.$numposts.')</a>' . category_description($catid) . '</li>';
    }
    };
    /* descriptive cat list  */
    1. So we get all the categories that are not empty.
    2. We make a variable that is each categories ID.
    3. Then echo out a list item for each one inside a link.
    4. Lastly, add the description using our variable for the ID.

    Then add this to the template file to call the function:

    <ul>
    <?php desc_cats(); ?>
    </ul>

    By default the “category_description()” wraps the description in side a P tag. If there is no description, it inserts a break tag instead.

    Thread Starter ralph23

    (@ralph23)

    Thanks man, that was perfect.

    How do I have it not display the break tag if there isn’t a description?

    Does it matter where in the function file I place that code?

    Not really, but I like putting them at the top.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display Categories and Descriptions’ is closed to new replies.