Code: Select all
$args = array(
'parent' => 0,
'orderby' => 'name',
'order' => 'ASC'
);
$parent_categories = get_categories($args);
foreach ($parent_categories as $parent_category) {
echo $parent_category->name;
echo '';
< /code>
Und das gibt mir < /p>
Category A
Category B
Category that has no children
Category D
Code: Select all
$termchildren = get_term_children( $term_id, 'category' );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, 'category' );
echo $term->name;
echo '';
Code: Select all
Child of Category B
< /code>
oder < /p>
Child of Category A
Child of Child of Category A
< /code>
Aber wie kann ich jede $ parent_category id als $ term_id an get_term_children (in, ich nehme an, eine Foreach -Schleife) übergeben.Category A
Child of Category A
Child of Child of Category A
Category B
Child of Category B
Category that has no children
Category D
Child of Category D
< /code>
Aktualisierung 5/13/25 < /strong> < /p>
Kaltis Antwort funktioniert großartig. Nun eine Follow -up -Frage :)
Wie würde ich Listen der Post -Titel erhalten, die in jeder Kategorie und in allen ihren untergeordneten Kategorien enthalten sind? D.h., < /p>
$args = array(
'post_type' => array('post','book','video'),
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_status'=> 'publish',
);
$cat_posts = new WP_Query($args);
while ($cat_posts->have_posts()) {
$cat_posts->the_post();
the_title();
echo '
';
}