Listen Sie alle Unterkategorien aus der Kategorie
-
-
Wie hiererläutert,können Sie alternativget_terms () verwenden: 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 Antworten
- Stimmen
-
- 2011-03-30
Ja,Sie können das get_categories () mithilfe des Attributs
'child_of'
verwenden. Zum Beispiel alle Unterkategorien der Kategoriemit der 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>'; }
Dadurch werden alle Kategorien abgerufen,die Nachkommen sind (d. h. Kinder und Enkelkinder).
Wenn Sienur Kategorien anzeigenmöchten,die direkte Nachkommen sind (d. h.nur Kinder),können Sie das Attribut
'parent'
verwenden.$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>'; }
-
** Nurein Vorschlag: ** Angesichts der Beliebtheit vonbenutzerdefinierten Beitragstypen und Taxonomienistesmeiner Meinungnachbesser,"get_terms" vorzuschlagen,da dies dazubeiträgt,Benutzermit allgemeinen Funktionen zum Abrufen von Begriffen vertraut zumachen,bei denen die Kategoriefunktionenetwas spezifisch sindauf dieeingebaute Taxonomie (wenn auchnichtin allen Fällen).Dumusstnatürlichnicht zustimmen,esistnurein Vorschlag ...;)**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
-
Ichbin damiteinverstanden,dass [get_terms ()] (https://developer.wordpress.org/reference/functions/get_terms/)möglicherweisebesserist.I agree that [get_terms()](https://developer.wordpress.org/reference/functions/get_terms/) might be better.
- 2
- 2016-04-04
- Django Reinhardt
-
@t31os - könntest dubitte eine Antwortmit `get_terms`posten?@t31os - could you post an answer using `get_terms` please?
- 0
- 2018-03-30
- vsync
-
Bittebeachten Sie,dass Sie standardmäßig keine Kategorienerhalten,denen kein Beitrag zugeordnetist.Verwenden Sie `hide_empty=>false`,um alle Kategorien abzurufen (gemäß derneuesten Version von 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
Wie kannich alle Unterkategorieneinerbestimmten Kategorie abrufen?