列出类别
-
-
如此处所述,您也可以使用get_terms():https://stackoverflow.com/questions/22443352/how-to-get-sub-categories-by-parent-category-id-in-wordpressAs explained here, you can alternatively use get_terms(): https://stackoverflow.com/questions/22443352/how-to-get-sub-categories-by-parent-category-id-in-wordpress
- 0
- 2019-08-07
- retroriff
-
1 个回答
- 投票数
-
- 2011-03-30
是的,您可以通过
'child_of'
属性使用get_categories(). 例如,ID为17的类别的所有子类别:$args = array('child_of' => 17); $categories = get_categories( $args ); foreach($categories as $category) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; echo '<p> Description:'. $category->description . '</p>'; echo '<p> Post Count: '. $category->count . '</p>'; }
这将获取所有后代类别(即子孙).
如果您只想显示直接后代的类别(仅子类别),则可以使用
'parent'
属性.$args = array('parent' => 17); $categories = get_categories( $args ); foreach($categories as $category) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; echo '<p> Description:'. $category->description . '</p>'; echo '<p> Post Count: '. $category->count . '</p>'; }
Yes, you can use get_categories() using
'child_of'
attribute. For example all sub categories of category with the ID of 17:$args = array('child_of' => 17); $categories = get_categories( $args ); foreach($categories as $category) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; echo '<p> Description:'. $category->description . '</p>'; echo '<p> Post Count: '. $category->count . '</p>'; }
This will get all categories that are descendants (i.e. children & grandchildren).
If you want to display only categories that are direct descendants (i.e. children only) you can use
'parent'
attribute.$args = array('parent' => 17); $categories = get_categories( $args ); foreach($categories as $category) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; echo '<p> Description:'. $category->description . '</p>'; echo '<p> Post Count: '. $category->count . '</p>'; }
-
**仅建议:**随着自定义帖子类型和分类法的流行,我建议建议使用get_terms会更好,因为这有助于使用户熟悉通用术语提取功能,而类别功能有些特定内置分类法(尽管并非在所有情况下).当然,您不必同意,这只是一个建议...;)**Just a suggestion:** With the popularity of custom post types and taxonomies, i feel it would be better to be suggesting `get_terms`, because this helps familiarise users with general term fetching functions, where as the category functions are somewhat specific to the built-in taxonomy(though not in all cases). You don't have to agree of course, it's just a suggestion... ;)
- 6
- 2011-03-30
- t31os
-
我同意[get_terms()](https://developer.wordpress.org/reference/functions/get_terms/)可能会更好.I agree that [get_terms()](https://developer.wordpress.org/reference/functions/get_terms/) might be better.
- 2
- 2016-04-04
- Django Reinhardt
-
@t31os-您可以使用`get_terms`发布答案吗?@t31os - could you post an answer using `get_terms` please?
- 0
- 2018-03-30
- vsync
-
请注意,默认情况下,这不会获得没有相关帖子的类别.使用`hide_empty=>false`获取所有类别(根据wordpress的最新版本)please note, by default, this will not get you categories that has no post associated with them. use `hide_empty => false` to get all categories (as per the recent version of wordpress)
- 1
- 2020-02-04
- swadhwa
如何从某个类别中获取所有子类别?