自定义帖子类型中自定义分类的条件标签 [英] Conditional Tags for Custom Taxonomy in Custom Post Type

查看:161
本文介绍了自定义帖子类型中自定义分类的条件标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望您对此给出一个切实可行的答案(可能对您来说是个简单的问题)。我创建了一个名为(例如:) 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';
}

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

For your reference, I've read this (http://wordpress.org/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

这篇关于自定义帖子类型中自定义分类的条件标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆