Wordpress - 父条款的条件

时间:2017-03-29 10:58:58

标签: php css wordpress html5

我创建了一个自定义帖子类型" Produit"使用分类法" Produits"。 在其中我创建了三个父母术语(" Securite"," Confort"," Fermeture")这三个父母术语有子女期限,所有子学期都有帖子。

enter image description here

在我的主页中,我显示了我所有的孩子名词

enter image description here

当我点击某个项目时,我会在子项网页中重定向(例如:' produits / securite / alarme /')。我的问题是三个父母术语在子术语页面上显示三种不同的颜色。

"SECURITE" -> orange
"CONFORT" -> pink
"FERMETURE" -> blue

所以我需要知道子术语的父术语是什么,以在页面中添加好颜色。

enter image description here

我搜索了wordpress条件,但我找不到解决方案。 我不知道我的解释是否清楚但有人知道我怎么能做到这一点?谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用以下命令检索父分类:

// Get the child term
$child = get_term($id, 'produits');

// get_term returns an object with "parent" as a variable.
// the "parent" variable will return the parent term ID, or 0 if there is no parent.
$parent = ($child->parent == 0) ? $child : get_term($child->parent, 'produits');

您还可以使用wp_get_post_terms( $post_id, $taxonomy, $args );来检索帖子的条款。从那里你可以循环使用返回的术语,并使用上面提到的方法找到父母。

WP Codex get_term()

WP Codex wp_get_post_terms()

相关问题