determining child categories and displaying their posts only: FUN!
-
I’m a fairly new coder, and I’ve been working on this one for days. I figured out one part of it, but I’m stumped on the other part. Any help would be appreciated.
It’s kind of complex. I’ll try to be as clear as possible.
The Goal:
I want to display only posts from categories that are children of a parent category I have named “Portfolio”.Now, I figured out how to do this using foreach() and an array defining the category IDs of the children categories I want to display. Like this:
<?php $children_cats = array(3,6,7); /*3,6,7 are the IDs of the child categories I want to display */ foreach ($children_cats as $my_cats) { query_posts("showposts=3&cat=$my_cats"); ?> while (have_posts()) : the_post(); ?>
Works like a champ, looping thru the array and displaying posts only from category IDs 3, 6 and 7.
However, I do not want to hard code those category IDs into that array. Over time, the list of categories that are children of Portfolio will be added to and subtracted from by another user. I don’t want to have to recode the template page every time something changes. Therefore, I need some code that will (1) return a list of categories that are children of the Portfolio categories, and (2) loop thru that list to display the category title and posts from each child category.
Just to be clear, the resulting WP page content would look something like this:
Child Category #1 Title
Child Category #1 Post
Child Category #1 Post
Child Category #1 Post
etcChild Category #2 Title
Child Category #2 Post
Child Category #2 Post(continuing for as many categories exist as children of the Portfolio category)
I thought I could do this using wp_list_categories, but I go stumped. I’ve been all over the codex, so if you’ve got an idea please talk me through it–don’t send just a link with no explanation or I’m afraid it will be no help! ??
Thanks! WP is awesome and the WP community is amazing!
-c.
- The topic ‘determining child categories and displaying their posts only: FUN!’ is closed to new replies.