自定义帖子类型中自定义分类的条件标签

编程入门 行业动态 更新时间:2024-10-27 01:24:56
本文介绍了自定义帖子类型中自定义分类的条件标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望您对此给出一个切实可行的答案(可能对您来说是个简单的问题)。我创建了一个名为(例如:) Cuspost的自定义帖子类型,并且在名为 Custax的Cuspost中创建了自定义分类法。然后我有了分类法:在Custax中有 A Custax和 B Custax。

I do hope you give me a practical answer for this (maybe simple problem for you). I have crated a Custom Post Type named (example:) "Cuspost" and I have Custom Taxonomy inside Cuspost named "Custax". Then I have taxonomies: "A Custax" and "B Custax" inside Custax.

我想做的就是例如检查Custax的值。与 has_custax('a-custax')(类似于 has_category('a-category'));

What I want to do is just want to check the value of the Custax, for example with has_custax('a-custax') (similar to has_category('a-category'));

接下来的用途是

<?php if (has_custax('a-custax')) { echo 'do something A'; } else { echo 'do something B'; }

供您参考,我已阅读此内容( wordpress/support/topic/custom-taxonomies-conditional-tags#post -1110167 ),它并不起作用。

For your reference, I've read this (wordpress/support/topic/custom-taxonomies-conditional-tags#post-1110167) and it aint work.

感谢帮助。

推荐答案

在 functions.php 上解决此功能,类似于Justin Tadlock解决方案

Solve with this function on functions.php similar to Justin Tadlock solutions

<?php function has_custax( $custax, $_post = null ) { if ( empty( $custax ) ) return false; if ( $_post ) $_post = get_post( $_post ); else $_post =& $GLOBALS['post']; if ( !$_post ) return false; $r = is_object_in_term( $_post->ID, 'custax', $custax ); if ( is_wp_error( $r ) ) return false; return $r; } ?>

这是条件标记。可以在循环内/循环外使用:

And this is the conditional tag. Can be used in/outside the loop:

<?php if ( has_custax( 'a-custax', $post->ID ) ) { echo 'do something A'; } else { echo 'do something B'; }; ?>

信用给我的朋友Sulton Hasanudin

Credit to my friend Sulton Hasanudin

更多推荐

自定义帖子类型中自定义分类的条件标签

本文发布于:2023-11-28 12:50:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   条件   类型   标签   帖子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!