Wordpress term_exist 没有回应任何东西? [英] Wordpress term_exist not echo'ing anything?

查看:33
本文介绍了Wordpress term_exist 没有回应任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力让它发挥作用.

I'm struggling to get this to work.

基本上,我正在运行 WP 查询以从我的自定义帖子类型中检索多个帖子.

Basically, I'm running a WP query to retrieve multiple posts from my custom post-type.

在循环中,我想有条件地回应内容,具体取决于分配给该帖子的术语.

In the loop, I want to conditionally echo stuff, depending on what term is assigned to that post.

我创建了一个名为文件类型"的分类法,并将其分配给我的自定义帖子类型.

I've created a taxonomy called 'file-types' and have assigned this to my custom post type.

据我所知,术语就像分类学的孩子.不是字面意思"

As I understand terms, terms are like the children of taxonomy. "Not literally"

我的问题,我下面的 PHP 似乎只是回应了最后一个条件,这没什么.

My problem, my PHP below just seems to echo the last condition, which is nothing.

而且我已 100% 将这些条款分配给我的帖子.

And I've 100% assigned these terms to my posts.

我唯一没有尝试过的是将term_taxonomy"更改为term_taxonomy_id" - 但我一生都无法找到如何找到我的分类法的 ID.

The only thing I have not tried is changing 'term_taxonomy' to a 'term_taxonomy_id' - but I cannot for the life of me find out how to find the ID of my taxonomy.


有人能帮我理解为什么没有回声吗?非常感谢

Can any one help me understand why nothing is being echo'ed? Many Thanks



<?php

    if (term_exists(array(

        'term_id'           => 4,
        'term_taxonomy'     => 'file-formats'

    ))) {

        echo 'PDF' ;

    }
    else if (term_exists(array(

        'term_id'           => 6,
        'term_taxonomy'     => 'file-formats'

    ))) {

        echo 'PPT' ;

    }
    else if (term_exists(array(

        'term_id'           => 5,
        'term_taxonomy'     => 'file-formats'

    ))) {

        echo 'MOV' ;

    }
    else {

        echo '' ;

    }

?>

推荐答案

问题是 term_exists() 除了像你拥有的数组一样.

The problem is that term_exists() doesn't except arrays like you have it.

有关如何使用它的详细信息,请参见此处:http://codex.wordpress.org/Function_Reference/term_exists

See here for details on how to use it: http://codex.wordpress.org/Function_Reference/term_exists

这就是它需要的样子:

 if( term_exists(5, 'file-formats') ) { 

 } elseif( term_exists(6, 'file-formats') ) { 

 } else { 

 }

我认为您也可以输入 slug 或名称:

I think you can also feed in a slug or name too:

term_exists('PDF', 'file-formats')

这篇关于Wordpress term_exist 没有回应任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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