WordPress.org

Ready to get started?Download WordPress

Forums

Displaying the category children of ONE category (7 posts)

  1. dave_merwin
    Member
    Posted 8 years ago #

    I have been trying to figure it out forever. I made it WAY more complicated then I needed to. SO if anyone wants to display the children of just ONE category, here is a way to do it. This will obviously be a list.

    Maybe I’ll write it as a plug-in when I have time. Like next year!

    <ul>
    <?php

    $get_children = $wpdb->get_results('SELECT * FROM dj_categories WHERE category_parent = "THE cat_ID of the PARENT that you want"');

    foreach ($get_children as $child) {
    echo '<li><a href="./?cat='.$child->cat_ID.'" title="View all posts filed under '.$child->cat_name.'">'.$child->cat_name.'</a></li>';
    }

    ?>
    </ul>

  2. Michael Bishop

    Posted 8 years ago #

    can you clarify "dj_categories"?

  3. Grievance
    Member
    Posted 8 years ago #

    Where exactly do you implement this?

  4. dave_merwin
    Member
    Posted 8 years ago #

    Oops Sorry. Try this...

    <ul>
    <?php
    $get_children = $wpdb->get_results('SELECT * FROM your category table WHERE category_parent = "The cat_ID of the parent that you want"');
    foreach ($get_children as $child) {
    echo '<li><a href="./?cat='.$child->cat_ID.'" title="View all posts filed under '.$child->cat_name.'">'.$child->cat_name.'</a></li>';
    }
    ?>
    </ul>

    I used it as a side bar to display the children of a parent category. It generates a list of the children of the parent.

  5. Bricolou
    Member
    Posted 8 years ago #

    Wow, this is just what I was looking for. Is there a way a sort the list of child categories, by alphabetical order for example?

  6. Kafkaesqui

    Posted 8 years ago #

    "Is there a way a sort the list of child categories, by alphabetical order for example?"

    Change the query to:

    $get_children = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE category_parent = 'The cat_ID of the parent that you want' ORDER BY cat_name ASC");

  7. Bricolou
    Member
    Posted 8 years ago #

    Thanks, Kafkaesqui, that works perfectly.

Topic Closed

This topic has been closed to new replies.

About this Topic