WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying category descriptions? (7 posts)

  1. CharlesKrause
    Member
    Posted 8 years ago #

    I've been tinkering with the template tags, but I don't see a way to do this...

    I have PHP execution enabled in static pages (using a plugin). What I would like to do is automatically generate the content of the page where you have something like this

    Name_of_Categoy_#1 <-- this is a link
    Description_of_Category_#1
    Name_of_Categoy_#2 <-- this is a link
    Description_of_Category_#2
    Name_of_Categoy_#2 <-- this is a link
    Description_of_Category_#2

    Listing a list of names which are links is dead simple - there are built in template tags that do that - but how about extracting and interleaving the stored description of the category?

  2. wyrd33
    Member
    Posted 8 years ago #

  3. CharlesKrause
    Member
    Posted 8 years ago #

    Kind of - save that the category_description(category) function takes the ID of the category as a parameter - and I don't have those :)

    The closest useful function I can find is get_the_category() - which returns an array of arrays of data elements about a category, one for each category that a POST IN THE LOOP belongs to. THAT I could pull apart and format out into the kind of list I want - BUT what I want is not the categories belonging to any one post, but a universal list off all categories belonging to the Blog - sort of a Category index with built in descriptions - automatically generated.

    Title/Link
    Description

    Title/Link
    Description

    etc.

  4. CharlesKrause
    Member
    Posted 8 years ago #

    Here we go - a nice little code snippet I came up with, dropeed into a page with the RunPHP plugin active for the page.


    <?php
    global $wpdb ;
    $results = $wpdb->get_results("SELECT * FROM " . $wpdb->categories);
    if ($results)
    {
    echo "<dl class=\"catdeflist\">n";
    foreach ($results as $row)
    {
    echo "<dt class=\"catdeflist_name\"><a href=\"/?cat=". $row->cat_ID . "\">". $row->cat_name . "</a></dt>n";
    echo "<dd class=\"catdeflist_desc\">". $row->category_description . "</dd>n";
    }
    echo "<dl>n";
    }
    else
    {
    echo "<p>There are no categories defined on this blog.</p>/n";
    }
    ?>

    It's even CSS styled. You can define 3 classes in your theme

    catdeflist - the definition list which holds all definitions and descriptions

    catdeflist_name - the '<dt></dt>' container which holds the name of the category.

    catdeflist_desc - the '<dd></dd>' container which holds the decription text of the category.

  5. wyrd33
    Member
    Posted 8 years ago #

    Hmm.. I'm actually quite surprised to not find a function that gives you an array of category objects based on what you specify. I guess I'll have to make a function based on wp_list_cats() and just return the category array.

  6. Anonymous
    Unregistered
    Posted 7 years ago #

    Thanks wyrd! I've read it, and it was really useful.

  7. catzavencu
    Member
    Posted 7 years ago #

    Is there a way Charles to use function include so you can list only certain categories with description under?

Topic Closed

This topic has been closed to new replies.

About this Topic