WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List Children Categories on Parent Category Page (3 posts)

  1. samanime
    Member
    Posted 4 years ago #

    I have a bunch of categories which have children categories within them.

    Right now, if I try to visit the page for a parent category all it displays is "No topics found". What I'd like to have happen is that if it's a parent category, it would list all of it's children categories, and possibly even nested list with all of the topics in the category (just the title).

    Anyone have ideas about how to get started? I don't need the details involved, just an idea of what functions I can use to get the information I want.

    Thanks.

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    get_query_var('cat'); // The current category ID being queried.

    http://codex.wordpress.org/Function_Reference/get_term - to get information on the category ...

    If the parent is 0, it's top level...

    http://codex.wordpress.org/Function_Reference/get_terms
    Use to get categories, use the "parent" parameter ...

    Check the parent, then query for categories based on what the parent is, ie. if it's 0 then you know it's top level..

    Does that help?.. you said you just needed some ideas of where to start.

    NOTE: 2 functions above do differ, notice the names, get_term and get_terms

  3. samanime
    Member
    Posted 4 years ago #

    Thanks for the help. After I posted this I went and experimented and got it working on my own.

    I used get_categories("child_of=" . $parentID) to get the children categories, then used the following code block to get it to print out how I wanted:

    foreach($children as $child) {
    		echo "
    <li><h2>" . htmlentities($child->name) . "</h2>";
    		echo "
    <ul>";
    		$posts = get_posts('cat=' . $child->cat_ID . '&orderby=title&order=ASC&numberposts=-1');
    		foreach($posts as $post)
    			echo "
    <li><a>post_ID) . "\">" . htmlentities($post->post_title) . "</a></li>
    ";
    		echo "</ul>
    ";
    		echo "</li>
    ";
    	}

    Hopefully that'll help anyone trying to do this in the future.

    EDIT: Oh, I also ended up getting the category I was using with:
    $cat = get_category_by_path($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'],false);

    Though, actually, I think I'm gonna switch it to your method. I like it better. =p

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.