php - Wordpress - 条件与父术语

标签 php css wordpress html

我创建了一个自定义帖子类型“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/

相关文章:

css - 外部 CSS 文件未在浏览器中加载

html - 从主页删除 Logo - Wordpress

Wordpress 类别固定链接

php - 电子邮件附件无法正确发送

javascript - Sucuri 404javascript.js 安全问题或内部服务器错误

php -/e 修饰符已弃用,从 php5.2.6 迁移后改用 preg_replace_callback

html - 将粗体悬停在单元格内容上时,表格单元格正在扩展

html - 在容器 div 内水平居中 div

css - 在 wordpress 专栏中写一些自定义 css

php - 仅在机器空闲时运行 cron 作业 (linux)