2012-03-10 123 views
0

这是我遇到的这样一个基本问题,但我已经尝试了这么多不同的功能,并且不能工作。Wordpress条件语句的分类,术语,在循环内不起作用

在我的wordpress网站,我创建了一个名为“文件格式

在这个分类我已经创建了这些条款(我认为这是项?)分类...... PDFMOVPPT及以下

见截图DOC ...

enter image description here


我有我似乎无法弄清楚的问题是我有一个简单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(); ?> 


...在这个循环中,我想有条件地显示一个图标,这取决于t恩姆已被分配到该职位。

例如,如果一个职位已分配“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' ; 
    } 
?> 


任何帮助将是惊人的,因为这似乎是这么辛苦,当它是一个faily基本的东西。

在此先感谢。



回答

0

我认为term_exists()抛出你了;检查给定分类标准是否包含x术语。如果您想要与给定帖子相关的条款,请使用get_the_terms(),它将返回一个数组。然后,您的代码可以检查该阵列以确定要插入哪个图像。

+0

这就是我想要做的,我试图检查该帖子是否有_x_字词,如果确实有这个字词,那么回显图片。 – Joshc 2012-03-10 22:19:31

+0

我试图实现的是类似于'in_category',这是一个类似于上面的方法。但用分类学来代替。 – Joshc 2012-03-10 22:24:22

+0

我想通了,我需要使用'has_term' – Joshc 2012-03-10 22:43:48