2012-03-10 33 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