2017-04-25 12 views
0

私の疲れた目は何かを失っていて、新しい眼球のペアは私が逃しているものを捕まえるかもしれないと思っています。Wordpress Taxonomy Archiveバグ

私は、residential_projectsのカスタムポストタイプに割り当てられた 'residential_project_types'のスラッグを持つカスタムタクソノミーを持っています。タクソノミからすべての用語を表示し、用語名とリンクを出力したいと思います。 ...

の代わりにそれぞれのための単一の用語を表示する作業の

その種、それは用語に含まれるすべてのポストのための用語が表示されるように表示されます。これはもちろん重複を作成しています。また、HTMLが正しく表示されず、要素が奇妙に重なり合っています。

私の勘違いはループで台無しになっています... ...?しかし、それを把握することはできませんでした。誰もが助けてくれてありがとう!ここで

は壊れた/バギーページへのリンクです: http://desarch.robertrhu.net/residential/

ここで私が書いたコードです:

<?php 
    $terms = get_terms(array(
     'taxonomy' => 'residential_project_types', 
     'orderby' => 'count', 
     'hide_empty' => false, 
     'fields'  => 'all' 
    )); 
?> 

<?php 
    foreach($terms as $term) { 

    $args = array(
     'post_type' => 'residential_projects', 
     'residential_project_types' => $term->slug 
    ); 

    $term_link = get_term_link($term); 

    $query = new WP_Query($args); 

    if ($query->have_posts()) : 
     /* Start the Loop */ 
     while ($query->have_posts()) : $query->the_post(); ?> 
     <a class="property-thumb-link" 
      href="<?php echo $term_link; ?>"> 
      <div class="property-thumb column medium-6 small-12"> 

       <img src="<?php the_field('category_image', $term); ?>" 
        alt="<?php the_field ('category_image_alt', $term); ?>" /> 

       <div class="property-thumb-title"> 
        <h2> 
         <?php echo $term->name; ?> 
        </h2> 
       </div> 
      </div> 
     </a> 
    <?php wp_reset_postdata(); 
    endwhile; 
endif; }?> 

答えて

0

をカスタム分類から用語を表示するには、私はあなたがすべきとは思いません投稿をループするためのWP_Query()を使用してください。

代わりにあなたは以下に示すように、それらをループ、get_terms()機能を使用して、あなたの長期的なオブジェクトを取得することができます。

<?php 
$terms = get_terms(array(
    'taxonomy' => 'residential_project_types', 
    'hide_empty' => false, 
)); 

// Temp output of terms so you can see what you're working with 
// var_dump($terms); 

// loop through all terms 
foreach($terms as $term) { 

    if(0 === $term->count) { 

     // This $term has no posts attached to it - you may want to treat it differently. 
     echo $term->name; 

    } elseif($term->count > 0) { 

     // Build your term markup for terms that have posts attached 
     $term_link = get_term_link($term); 
     ?> 
     <a class="property-thumb-link" href="<?php echo $term_link; ?>"> 
      <div class="property-thumb column medium-6 small-12"> 
       <img src="<?php the_field('category_image', $term->term_id); ?>" alt="<?php the_field ('category_image_alt', $term->term_id); ?>" /> 
       <div class="property-thumb-title"> 
        <h2><?php echo $term->name; ?></h2> 
       </div> 
      </div> 
     </a> 
     <?php 

    } 

} 

ます。またget_terms()'hide_empty' => trueを設定することで、空の条件を隠すことができ - あなたは、その条件のチェックをドロップすることができ空の言葉のために。

ここでは、ACFのカスタムフィールドが用語に付けられていると仮定していますので、第2引数として$term->term_idthe_field()に渡す必要がありますが、これらのフィールドで何をしているのかは分かりません。うまくいけば、これは進歩を助けるのに十分です。

幸運を祈る!

+0

お返事ありがとう!サイコロはありません。私も同様の考えを持っていました。リセットを複数の場所に移動し、変更はしません。 – user5176291

+0

こんにちは - 私はあなたがしようとしていることを誤解していると思います。私は私の答えを修正します! – DavidCara

0

私はこれを非常に複雑にしていました。ここに私の答えです...

<?php $terms = get_terms(array(
    'taxonomy' => 'residential_project_types', 
    'orderby' => 'count', 
    'hide_empty' => true 
)); 

    foreach($terms as $term) : 
?> 

<a class="property-thumb-link" 
    href="<?php echo get_term_link($term); ?>"> 
    <div class="property-thumb column medium-6 small-12"> 

     <img src="<?php the_field('category_image', $term); ?>" 
      alt="<?php the_field ('category_image_alt', $term); ?>" /> 

     <div class="property-thumb-title"> 
      <h2> 
       <?php echo $term->name; ?> 
      </h2> 
     </div> 
    </div> 
</a> 
<?php endforeach; ?> 
関連する問題