WordPress.org

Ready to get started?Download WordPress

Forums

Categories and Descriptions (6 posts)

  1. Erum
    Member
    Posted 8 years ago #

    I want to display a list of categories along with their descriptions on the main page. I have looked and I have looked and all I can find are loops to display categories only or descriptions for single categories only.

    What I need is a loop that displays category - Description . I have managed to do this using the following code. However, I need to cut out the categories without posts in them. How do I do that? Or is there a plugin that I can use but haven't found yet :)

    <?php
    $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename, category_description, category_parent FROM $wpdb->categories
    WHERE cat_ID > 0 $exclusions
    ORDER BY cat_name"); ?>

    <?
    foreach($categories as $np){
    print ("<h2><a href=\"");
    echo get_category_link($np->cat_ID);
    print ("\">$np->cat_name</h2><p style=\"font-size: 90%; padding-top:0px; margin-top:0px;\">
    $np->category_description");
    }

    ?>

    ----------------------
    Q2 From what I can see using the tag: <?php the_category(', ') ?> is supposed to seperate the categories with a ',' which I take to mean that it should display as: Parent Category, Sub Category, Sub Sub Category.
    But I only get a single category for the Sub Category and Sub Sub Category.

  2. jwurster
    Member
    Posted 8 years ago #

    I think you want to use:

    <?php list_cats(1, 'all', 'name', 'asc', '', 1, 0, 1, 0, 0, 1, 0,0,'','','','',1) ?>

  3. Erum
    Member
    Posted 8 years ago #

    That gives me a list of all the categories and subcategories.

    What I want is a loop of Categories and their Descriptions (and possibly a list of sub categories)

    Eg:
    Category A
    Description of Category A
    Sub Categories: Sub Cat1 | Sub Cat2 | Sub Cat3

    Category B
    Description of Category B
    Sub Categories: None

    ---------------
    For the second option I need it to display the trail to the subcategory. So on Category A page it should just show Category A as the heading, on its sub category page it should show this trail:
    Category A < Sub Cat1 (going to as many levels as are present.)

    Hope that makes it clear. Thanx!

  4. boby
    Member
    Posted 8 years ago #

    Hello! I was just looking for something like this but I haven't found, so I wrote a simple script. You have probably to change some things to fit your needs. This is just questions 1, for question 2 you must do that or wait for another post :]

    <?php
    $main_categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename, category_description, category_parent FROM ".$wpdb->categories." WHERE category_parent=0 ORDER BY cat_name");

    foreach($main_categories as $main_cat)
    {
    ?>
    <h2 class="category_name">cat_ID; ?>" title="Browse category <?php echo $main_cat->cat_name; ?>"><?php echo $main_cat->cat_name; ?></h2>
    <p class="category_description"><?php echo $main_cat->category_description; ?>

    <?php
    $sub_categories = $wpdb->get_results("SELECT cat_ID, cat_name FROM ".$wpdb->categories." WHERE category_parent=".$main_cat->cat_ID." ORDER BY cat_name"); ?>
    <?php if( isset($sub_categories) && !empty($sub_categories) )
    { ?>
    <p class="category_subcategories">Subcategories:
    <?php
    foreach( $sub_categories as $sub_cat )
    {
    ?>
     cat_ID; ?>" title="Browse category <?php echo $sub_cat->cat_name; ?>"><?php echo $sub_cat->cat_name; ?> *
    <?php
    }
    } ?>

    <?php
    }
    ?>

    This will display only subcategories if there are some available.

    Eg:
    Category A
    Description of Category A
    Sub Categories: Sub Cat1 | Sub Cat2 | Sub Cat3

    Category B
    Description of Category B

    Cheers, Boby

  5. Erum
    Member
    Posted 8 years ago #

    Thanks!! I'll try that out!

  6. erb
    Member
    Posted 8 years ago #

    Just for anyone else who stumbles across this: I needed a simple page listing all the categories and their descriptions with the category names linked to their archives. I had a bit of a frustrating time finding the category equivalent of get_permalink (it's get_category_link).

    Here is the simple code I use (I use it in a custom Page):

    <?php
    $results = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename, category_description, category_parent FROM ".$wpdb->categories." ORDER BY cat_name");
    ?>
    <dl>
    <?php foreach($results as $cat){ ?>
    <dt><a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->cat_name; ?></a></dt>
    <dd><?php echo $cat->category_description; ?></dd>
    <? }?>
    </dl>

Topic Closed

This topic has been closed to new replies.

About this Topic