Wordpress conditional statements for taxonomy, term, inside a loop not working

我的梦境 提交于 2019-12-06 14:54:43

问题


This is such a basic problem I'm having but I've tried so many different functions and can't get any to work.

On my wordpress website I have created a taxonomy called 'file-formats'

and in this taxonomy I have created these terms (I think these are terms?)... PDF, MOV, PPT and DOC

See screenshot below...


The problem I have which I can't seem to figure out, is I have a simple WP_Query loop, see below...


<?php   
    $downloads  = new WP_Query(array(
        'post_type'         => 'downloads',
        'order'             => 'ASC',
        'posts_per_page'    => -1
)); ?>

<?php if ($downloads->have_posts()) : ?>

    <?php while ($downloads->have_posts()) : $downloads->the_post(); ?>

        <!-- This is where my conditional statement will go, see below -->

    <?php endwhile; ?>

<?php unset($downloads); endif; wp_reset_query(); ?>


...and inside this loop, I want to conditionally display an icon, depending on what term has been assigned to that post.

For example if a post has been assigned 'PDF' term, then I want my PDF icon image to be displayed, etc.

See below my conditional statement PHP, which sits within the loop above. But I can't figure out why the last condition is always being echoed. Help!


<?php
    if (term_exists(array(
        'term_id'           => 4,
        'term_taxonomy'     => 'file-formats'
    ))) {
        echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/pdf.png" alt="" class="file-format-icon">' ;
    }
    else if (term_exists(array(
        'term_id'           => 6,
        'term_taxonomy'     => 'file-formats'
    ))) {
        echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/ppt.png" alt="" class="file-format-icon">'    
    }
    else if (term_exists(array(
        'term_id'           => 5,
        'term_taxonomy'     => 'file-formats'
    ))) {
        echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/mov.png" alt="" class="file-format-icon">' ;  
    }
    else {
        echo 'nothing' ;
    }
?>


and I've also tried this...


<?php
    if (is_tax('file-formats','pdf')) {
        echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/pdf.png" alt="" class="file-format-icon">' ;  
    }
    else if (is_tax('file-formats','ppt')) {
        echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/ppt.png" alt="" class="file-format-icon">' ;  
    }
    else if (is_tax('file-formats','mov')) {
        echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/mov.png" alt="" class="file-format-icon">' ;  
    }
    else {
        echo 'nothing' ;
    }   
?>


and this...


<?php
    if (is_object_in_taxonomy( 'pdf', 'file-formats' )) {
        echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/pdf.png" alt="" class="file-format-icon">' ;
    }
    else if (is_object_in_taxonomy( 'ppt', 'file-formats' )) {
        echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/ppt.png" alt="" class="file-format-icon">' ;  
    }
    else if (is_object_in_taxonomy( 'mov', 'file-formats' )) {
        echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/mov.png" alt="" class="file-format-icon">' ;
    }
    else {
        echo 'nothing' ;
    }
?>


Any help would be amazing because this seems to be so hard when it's a faily basic thing.

Thanks in advance.




回答1:


I think term_exists() is throwing you off; that checks if a given taxonomy contains x term, generally. If you want the terms associated with a given post, use get_the_terms(), which returns an array. Your code can then check against that array to decide which image to insert.



来源:https://stackoverflow.com/questions/9650485/wordpress-conditional-statements-for-taxonomy-term-inside-a-loop-not-working

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