2017-04-25 38 views
0

我希望只是我疲憊的眼睛缺少的東西和一個新的眼球可能趕上我失蹤。WordPress的分類存檔BUG

我有一個自定義分類與'residential_project_types'的slug分配給自定義職位類型的residential_projects。我想顯示分類中的所有術語,輸出術語名稱和鏈接。

其工作的實物......

而是顯示每個單一的名詞,這似乎是爲顯示包括在術語每一個崗位的術語。這當然是創造重複。另外,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自定義字段已附加到該術語,因此我們需要將$term->term_id作爲第二個參數傳遞給the_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; ?>