我有一个带有分类“部分”的自定义帖子类型,我有一个带有查询循环的模板页面,用于显示带有缩略图的类别列表。我只想显示当前父分类的子类别,并希望在子页面上能够很好地获取父类别的ID。
我目前已在我的代码中将父项设置为id 40,但需要它是动态的。如何将 40 动态更改为当前父 ID?
这是我在分类模板页面中的代码。
<?php
$terms = get_terms( [
'taxonomy' => 'section',
'parent' => 40,
'hide_empty' => true,
'relationship' => [
'id' => 'categories_to_posts',
'to' => get_the_ID(), // You can pass object ID or full object
],
] );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$term_link = get_term_link( $term->term_id );
$term_name = $term->name;
$url = get_term_meta( $term->term_id, 'kte_sec_thumbnail_image', true );
echo '<img src="' . esc_url( $url ) . '">';
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<a href="' . esc_url( $term_link ) . '">' . $term_name . '</a>';
}
}
慕工程0101907