Listar todas as subcategorias da categoria
-
-
Comoexplicado aqui,vocêpode alternativamente usarget_terms (): https://stackoverflow.com/questions/22443352/how-to-get-sub-categories-by-parent-wordress-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 responda
- votos
-
- 2011-03-30
Sim,vocêpode usar get_categories ()
'child_of'
atributo. Porexemplo,todas as sub-categorias de categoria com o ID de 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>'; }
Isso obterátodas as categorias que são descendentes (isto é,crianças & amp;netos).
Se você quiserexibir apenas as categorias que são descendentes diretas (somentefilhos) Vocêpode usar o atributo
'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>'; }
-
** Apenas uma sugestão: ** Com apopularidade dostipospersonalizadose taxonomias,sinto que seriamelhor ser sugerindo "get_terms",porqueisso ajuda afamiliarizar os usuários comfunções debusca determogeral,onde asfunções da categoria são umpoucoespecíficaspara ataxonomiaembutida (emboranãoem todos os casos).Vocênãoprecisa concordar,é claro,é apenas uma sugestão ...)**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
-
Eu concordo que [get_terms ()] (https://developer.wordpress.org/reference/functions/get_terms/)pode sermelhor.I agree that [get_terms()](https://developer.wordpress.org/reference/functions/get_terms/) might be better.
- 2
- 2016-04-04
- Django Reinhardt
-
@ T31OS - Vocêpoderiapostar uma resposta usando `get_terms`porfavor?@t31os - could you post an answer using `get_terms` please?
- 0
- 2018-03-30
- vsync
-
Porfavor,note,porpadrão,issonão receberá as categorias quenãotem post associada aelas.Use `hide_empty=>false`para obtertodas as categorias (conforme a versão recente do 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
Comoposso obtertodas as subcategorias de uma determinada categoria?