我有一個帶有分類“部分”的自定義帖子類型,我有一個帶有查詢循環的模板頁面,用于顯示帶有縮略圖的類別列表。我只想顯示當前父分類的子類別,并且希望能夠在子頁面上獲取父類別的 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>'; } }
1 回答

達令說
TA貢獻1821條經驗 獲得超6個贊
據我所知,您想在父分類頁面上顯示當前父分類的子術語。因此,使用此代碼來實現您的功能..
<?php
$term123 = get_queried_object();
$slug=$term123->slug;
$parent_id =$term123->parent;
$child_id=$term123->term_id;
$taxonomy_name ='section';
$termchildren = get_term_children( $child_id, $taxonomy_name );
echo "<ul>";
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
?>
<?php if ($term->parent == $child_id) { ?>
<li><a href="<?php echo get_term_link($term->term_id); ?>"><?php echo $term->name; ?></a></li>
<?php }
?>
<?php }
echo "</ul>";
?>
- 1 回答
- 0 關注
- 116 瀏覽
添加回答
舉報
0/150
提交
取消