WordPress.org

Ready to get started?Download WordPress

Forums

Using category_description () to display....the description of your category!! (4 posts)

  1. kalico
    Member
    Posted 8 years ago #

    This post contains a much-needed solution, and a question for further development of this concept. Please respond!

    SOLUTION FOUND:

    You'd think displaying your category descriptions would be the first thing you'd do with category_description () but it's not that easy, as evidenced by all the forum threads I found last night where the question, in one form or another, is repeatedly asked but never satisfactorily resolved. Those which appeared to be resolved, didn't work for me.

    I finally found the "missing link" to make this work. It might make the other solutions work to, if folks know this apparently important fact: category_description only works on an archive page -- that is, a Page, created using a specially designed archive template.

    Here is the code that worked for me. It's possible the other solutions offered would also work, in an archive template. Or maybe they would work on any Page, with a correctly designed template. Anyway, this is what I did.

    First I followed the instructions for creating an Archive Index, which is essentially creating a special Page template:

    http://codex.wordpress.org/Creating_an_Archive_Index

    On that template page, I included ONLY the core code to make any Page match my site, AND the code that someone had given on the forum for using category_description. (I truly wish I could give credit to the person who posted this. I have searched like crazy through my history and can't find the page. But this was the first piece of code I found that actually worked for this endeavor, and it made me realize I needed an archive page -- someone should get credit!!) BTW, I have a custom designed theme, so I didn't have an archive page -- until now.

    Here is the ENTIRE contents of my archive template (which is named archives.php and uploaded via FTP to my theme folder):

    <?php
    /*
    Template Name: Archives
    */
    ?>
    <?php
    get_header(); ?>

    <?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>

    <?php
    get_footer(); ?>

    [NOTE: My sidebar is contained in my footer, and hence is not called in this code. You may have other elements which need to be in your template, which are not in mine, but this should get you started.]

    Then, per the archive page creation instructions, I went to Admin >> Write >> Page and made a page which had a subject, but left the content area blank. I then chose the Archive template from the drop-down list of templates in the sidebar, and hit "save".

    Then I visited Manage >> Pages and clicked the View link for the new Archive page....VOILA!! I now have a page with all my categories -- and sub-categories -- listed and linkified, along with their descriptions.

    FOR FURTHER DEVELOPMENT:

    Now comes the question....I need to have more control over what is displayed on this page, so I'm hoping someone can direct me. Please remember, I didn't code this genius solution; I just put a few puzzle pieces together ... now I'm stuck again!

    What I want to do is show the category names and descriptions of only ONE category (category 28) and it's children, not all of them. How would I do that?

    I have tried including "WHERE cat_ID=28" just after the FROM statement, but that got me ONLY the parent category. I could list all the category IDs for all the children, but I'm hoping there is a more direct way to manage this.

    Thank you in advance to someone more experienced than me.....!

  2. macitect
    Member
    Posted 7 years ago #

    Thank you! This post was really helpful. Sorry I can't be the person more experienced than you to help address your question.

  3. richarduk
    Member
    Posted 7 years ago #

    Although I've never used it this plugin looks like it could do the trick

    http://www.dagondesign.com/articles/sitemap-generator-plugin-for-wordpress/

    As you correctly said you created a Page. A Page is essentially no different from the index.php

    The only difference is the Template name:whatever bit within php comment tags, and that the page is called using <?php wp_list_pages();?>

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    This is a bit inaccurate. You can call category_description($category) anywhere, but you do need to pass it the category ID number.

    So, in the loop, if you had a post in a bunch of categories, you could do something like this:

    <?php
    foreach((get_the_category()) as $cat) {
    category_description($cat)
    } ?>

    To display the descriptions for all those categories. Not that you'd want to, but still....

Topic Closed

This topic has been closed to new replies.

About this Topic