2012-03-10 4 views
0

これは私が持っているような基本的な問題ですが、私は非常に多くの異なる機能を試しています。私はタクソノミー、用語、ループ内のWordpressの条件文は機能しません

「をファイル形式」と呼ばれる分類法を作成し、この分類では、私はこれらの用語を作成している私のワードプレスのウェブサイトに

(私はこれらの用語だと思う?)... PDFMOVPPTDOC以下

を参照してくださいスクリーンショット...

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


...このループの内部では、条件に応じてアイコンを表示したいermはその投稿に割り当てられています。例えば

ポストは「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

+0

私が達成しようとしているのは、上記と同様の方法である 'in_category'に似ています。しかし、代わりにタクソノミで。 – Joshc

+0

私はそれを理解しました、私は 'has_term'を使用する必要があります – Joshc