Elenca tutte le sottocategorie dalla categoria
-
-
Come spiegato qui,puoiin alternativa utilizzareget_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 risposta
- voti
-
- 2011-03-30
Sì,puoi utilizzare get_categories () utilizzando l'attributo
'child_of'
. Adesempio,tutte le sottocategorie di categoria con 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>'; }
Otterrannotutte le categorie discendenti (adesempiofiglie nipoti).
Se desideri visualizzare solo le categorie che sono discendenti diretti (cioè solobambini),puoi utilizzare l'attributo
'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>'; }
-
** Solo un suggerimento: ** Con lapopolarità deitipi dipostpersonalizzatie delletassonomie,penso che sarebbemeglio suggerire `get_terms`,perché questo aiuta afamiliarizzaregli utenti con lefunzionigenerali di recupero deitermini,dove lefunzioni di categoria sono alquanto specificheallatassonomiaincorporata (sebbenenonin tuttii casi).Ovviamentenon deviessere d'accordo,è solo un suggerimento ...;)**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
-
Sono d'accordo che [get_terms ()] (https://developer.wordpress.org/reference/functions/get_terms/)potrebbeesseremigliore.I agree that [get_terms()](https://developer.wordpress.org/reference/functions/get_terms/) might be better.
- 2
- 2016-04-04
- Django Reinhardt
-
@t31os -potrestipubblicare una risposta usando `get_terms`perfavore?@t31os - could you post an answer using `get_terms` please?
- 0
- 2018-03-30
- vsync
-
tienipresente che,perimpostazionepredefinita,questononti porterà a categorie a cuinon sono associatipost.usa `hide_empty=>false`per otteneretutte le categorie (comeper la versione recente di 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
Comeposso otteneretutte le sottocategorie di una determinata categoria?