gpt4 book ai didi

php - Wordpress - 条件与父术语

转载 作者:行者123 更新时间:2023-11-28 04:03:02 25 4
gpt4 key购买 nike

我创建了一个自定义帖子类型“Produit”,分类法为“Produits”。在其中我创建了三个父术语(“Securite”、“Confort”、“Fermeture”)这三个父术语有子术语并且所有子术语都有帖子。

enter image description here

在我的主页上我显示了我所有的子项

enter image description here

当我点击一个元素时,我将在子术语页面中重定向(示例 url:'produits/securite/alarme/')。我的问题是三个父术语在子术语页面上显示三种不同的颜色。

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

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

enter image description here

我在 wordpress 条件下进行了搜索,但没有找到解决方案。我不知道我的解释是否清楚,但有人知道我该怎么做吗?谢谢

最佳答案

您可以使用以下方法检索父分类:

// 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()

关于php - Wordpress - 条件与父术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43091622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com