我正在使用這两个函式,从而获得了自定義分類法的頂級父類別.這些已经過測試,可以按預期工作。
但是,現在我正在尋找一種輸出子類別而不是父類別的方法。
function get_term_top_most_parent( $term, $taxonomy ) {
// Start from the current term
$parent = get_term( $term, $taxonomy );
// Climb up the hierarchy until we reach a term with parent = '0'
while ( $parent->parent != '0' ) {
$term_id = $parent->parent;
$parent = get_term( $term_id, $taxonomy);
}
return $parent;
}
function get_top_parents( $taxonomy ) {
// get terms for current post
$terms = wp_get_object_terms( get_the_ID(), $taxonomy );
$top_parent_terms = array();
foreach ( $terms as $term ) {
//get top level parent
$top_parent = get_term_top_most_parent( $term, $taxonomy );
//check if you have it in your array to only add it once
if ( !in_array( $top_parent, $top_parent_terms ) ) {
$top_parent_terms[] = $top_parent;
}
}
// build output (the HTML is up to you)
foreach ( $top_parent_terms as $term ) {
//Add every term
$output = $term->slug;
}
return $output;
}
最新回復
- 6月前1 #
相似問題
- wordpress:wP_Query中每種帖子型別的不同選項wordpresscustomposttypeswordpressphpwordpresswpquerywordpresscustomtaxonomy2020-11-12 00:54
- wordpress:想要仅過濾管理區域中的父帖子wordpresscustomposttypeswordpresswpquerywordpresscustomtaxonomywordpressfilterswordpresswpdb2020-06-20 22:27
- wordpress:如何將2个引數插入1个wp_Query进行幻灯片顯示wordpresspostswordpresswpquerywordpresscustomtaxonomywordpresspageswordpressslideshow2020-05-25 19:28
- wordpress:列出分類術語以及按發佈日期排序的最新帖子wordpresswpquerywordpresscustomtaxonomywordpresstermswordpressorderwordpressdatetime2020-04-14 01:53
如果用於产品類別,則此功能仅需要類別ID即可知道其子類別:
可以這樣稱呼:
因為它可以返迴多个我们可以先檢查的
如果是這樣,我们只需要做一个foreach迴圈就可以全部获取。
這是我的代碼,以便在使用cat_childs()函式檢查是否需要下一个迴圈的第三代产品之前,使用它们全部製作選單。