WORDPRESS: Display list of Sub-Categories and the posts they contain, within one main Category
-
I've found tons of code and plugins to do various things; from show posts for specific cats, subcats of a cat, etc.. BUT, I cannot for the life of me find, nor do I know the WP API well enough to do what I need with it.. Here is what I'm trying to accomplish: Display a UL of all subcats within Cat31, and the posts for each of those subcats: SubCat1 Post 1 Post 2 SubCat2 Post 1 Post 2 SubCat3 Post 1 Post 2 It's pretty straight forward, but all the loops I have tried fail either at the subcat loop or the post loop (one or the other works, I cannot get them both to work..) So, unless I can find a plugin to do this (I'd prefer to code this into a template file!) then I need to figure out how to: Loop Subcats within Cat31 while looping subcasts, loop posts for each subcat Any help is GREATLY appreciated!
-
Answer:
I think here's what you need. $categories= get_categories('child_of=10'); for each $categories as $category { //Display the sub category information using $category values like $category->cat_name $posts_array = get_posts( 'category=$category->cat_ID' ); for each $posts_array as $post { //Display the posts information using $post values like $post->post_title } } You just need to format them. Hope this would be of help.
revive at Stack Overflow Visit the source
Other answers
@Free Lancer, Thanks again.. after working with your code for a bit, I was able to make it work. Some of the changes included: 'for each' changed to foreach foreach statements wrapped in () I also changed the 'category=$category->cat_ID' to 'cat='.$category->term_id lastly, I changed the foreach loop on the posts and added setup_postdata( $post ); to that loop. For all that need a snippet that will display a list of subcategories, within a specified category, AND show all the posts within each of those subcats.. here it is: $categories = get_categories('child_of=31'); foreach ($categories as $category) { //Display the sub category information using $category values like $category->cat_name echo '<h2>'.$category->name.'</h2>'; echo '<ul>'; foreach (get_posts('cat='.$category->term_id) as $post) { setup_postdata( $post ); echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>'; } echo '</ul>'; } Thanks again for all the help and pointing me in the right direction !!
revive
Related Q & A:
- How do I redirect all posts from the old Wordpress to a subdomain?Best solution by en.support.wordpress.com
- Why do Wordpress rewriites seem to work differently for posts vs pages?Best solution by WordPress
- How to change category of a post automatically in WordPress?Best solution by Stack Overflow
- how show posts in category?Best solution by WordPress
- How many advertisements are within one magazine generally?Best solution by studio1productions.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.