[英]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.. 从特定猫,猫的子猫等的表演帖子中获取。但是,我一辈子都找不到,也不知道WP API足以满足我的需要。
Here is what I'm trying to accomplish: 这是我要完成的工作:
Display a UL of all subcats within Cat31, and the posts for each of those subcats: 显示Cat31中所有子类别的UL,以及每个子类别的帖子:
SubCat1 Post 1 Post 2 SubCat1发布1发布2
SubCat2 Post 1 Post 2 SubCat2发布1发布2
SubCat3 Post 1 Post 2 SubCat3发表1发表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..) 这很简单,但是我尝试过的所有循环都在subcat循环或post循环上失败(一个或另一个起作用,我无法让它们都起作用。)
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 循环子广播时,在Cat31中循环子猫,为每个子猫循环帖子
Any help is GREATLY appreciated! 任何帮助是极大的赞赏!
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. 希望这会有所帮助。
@Free Lancer, Thanks again.. after working with your code for a bit, I was able to make it work. @Free Lancer,再次感谢..在使用您的代码一段时间后,我得以使其工作。 Some of the changes included: 一些更改包括:
'for each' changed to foreach
“每个人”更改为foreach
foreach
statements wrapped in () 包装在()中的foreach
语句
I also changed the 'category=$category->cat_ID' to 'cat='.$category->term_id
我也将'category=$category->cat_ID' to 'cat='.$category->term_id
lastly, I changed the foreach loop on the posts and added setup_postdata( $post );
最后,我更改了帖子上的foreach循环,并添加了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 !! 再次感谢您的所有帮助,并指出正确的方向!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.