【问题标题】:Wordpress conditional statements for taxonomy, term, inside a loop not working用于分类、术语、循环内的 Wordpress 条件语句不起作用
【发布时间】:2012-03-27 20:58:01
【问题描述】:

这是我遇到的一个基本问题,但我尝试了很多不同的功能,但都无法正常工作。

在我的 wordpress 网站上,我创建了一个名为“file-formats”的分类法

在这个分类中,我创建了这些术语(我认为这些是术语?)... PDF、MOV、PPT 和文档

请看下面的截图...


我似乎无法弄清楚的问题是我有一个简单的 WP_Query 循环,见下文...


<?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(); ?>


...在这个循环中,我想有条件地显示一个图标,具体取决于为该帖子分配的术语。

例如,如果帖子被分配了“PDF”术语,那么我希望显示我的 PDF 图标图像,等等。

请看下面我的条件语句 PHP,它位于上面的循环中。但我不明白为什么最后一个条件总是被回显。救命!


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


我也试过这个......


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


还有这个……


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


任何帮助都会令人惊叹,因为当它是一个失败的基本事物时,这似乎太难了。

提前致谢。



【问题讨论】:

    标签: wordpress conditional-statements


    【解决方案1】:

    我认为 term_exists() 让你失望了;通常检查给定分类法是否包含 x 术语。如果您想要与给定帖子关联的术语,请使用get_the_terms(),它会返回一个数组。然后,您的代码可以检查该数组以决定插入哪个图像。

    【讨论】:

    • 这就是我正在尝试做的,我尝试检查该帖子是否有 x 术语,如果确实有该术语,则回显图像。跨度>
    • 我想要达到的效果类似于 'in_category' ,这是与上面类似的方法。但是用分类法代替。
    • 我想通了,我需要使用'has_term'
    • 酷,这是一个比我建议的更直接的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多