Wordpress term_exist not echo'ing anything?

杀马特。学长 韩版系。学妹 提交于 2019-12-12 02:27:25

问题


I'm struggling to get this to work.

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"


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

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

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 '' ;

    }

?>

回答1:


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

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

This is how it needs to be:

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

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

 } else { 

 }

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

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


来源:https://stackoverflow.com/questions/9617533/wordpress-term-exist-not-echoing-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!