I am adding some custom code for a taxonomy.php WordPress-page. I do NOT know how to write php. This has taken me 3 days already, and i'm trying to customize it.
It is exactly how I want it now, except I only want to display direct children of the current taxonomy.
Can someone help me change this to show only direct children, not all children?
<?php
$term_id = get_queried_object()->term_id;
$taxonomy_name = get_query_var( 'taxonomy' );
$termchildren = get_term_children( $term_id, $taxonomy_name );
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$url = get_term_link( $child, $taxonomy_name );
$name = $term->name;
$image = get_field('taxonomy_image', $term );
$description = get_term_field( 'description', $term );
echo '<div class="sub-category">';
echo '<a href="' . $url . '">' . $name . '<div class="sub-category-image"><img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '" /></div><div class="sub-category-description">' . $description . '</div></a>';
echo '</div>';
}
?>
Background info (don't know if this is relevant):
It is a custom taxonomy "area" made with the plugin "CPT UI". The custom taxonomies also include custom fields from "Advanced Custom Fields". I want to show the taxonomies and not the posts on all taxonomies with children.
EDIT
I... "customized" the code according to the link provided for me in the comments, but as I said, PHP is not my language.
My code now looks like this:
<?php
$term_children = get_terms( 'produktkategori', array( 'parent' => get_queried_object_id(), ) );
if ( ! is_wp_error( $terms ) ) {
foreach ( $term_children as $child ) {
$image = get_field( 'taxonomy_image', $term );
$description = get_term_field( 'description', $term );
echo '
<div class="sub-category">
<a href="' . get_term_link( $child ) . '">
<div class="sub-category-image">
<img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '" /></div>
<div class="sub-category-description">
<h3>' . $child->name . '</h3>
' . $description . '
</div>
</a>
</div>
';
}
}
?>
Which in turn actually gave me only direct taxonomy children (which I wanted) but the image and description showing with the child taxonomies are THEIR child's taxonomy image and description. Sooo...
My Answer:
To display only the direct children of the current taxonomy, you can use the `get_terms()` function with the `parent` parameter set to the ID of the current term. Here's the updated code:
php
$term_id = get_queried_object()->term_id;
$taxonomy_name = get_query_var( 'taxonomy' );
$term_children = get_terms( array(
'taxonomy' => $taxonomy_name,
'parent' => $term_id,
) );
foreach ( $term_children as $child ) {
$image = get_field( 'taxonomy_image', $child );
$description = get_term_field( 'description', $child );
echo '
';
}
?>
In this code, we are using `get_terms()` to retrieve only the direct children of the current term. We then loop through these children and display their image and description. Make sure to replace `'produktkategori'` with your actual taxonomy name.
Rate this post
4 of 5 based on 9791 votesComments