2016-04-26 26 views
3

好的,這可能是一個簡單的。但我似乎無法找出出於某種原因。在循環中顯示自定義分類Wordpress

我有一個自定義的帖子類型叫做:Beachevents。 我有幾個活動。我也有一個名爲:Thema的自定義分類。

當做我的beachevent頁面(不是職位),我創建了一些類型的主題(主題)。比如:Strand Spellen(slu is子彈)。

現在我想製作一個循環,顯示的只有縮略圖和所有東西的鏈式拼圖。

有沒有人知道我是如何去做這件事的?

我嘗試了一些這樣的代碼,但不要做伎倆。

$args = array(
      'post_type' => 'beachevents', 
      'posts_per_page'=> -1, 
      'tax_query' => array(
       array(
        'taxonomy' => 'strand-spellen', 
        'field' => 'slug', 
        'terms' => 'all' 
       ) 
      ) 
     ); 
     $products = new WP_Query($args); 
     if($products->have_posts()) { 
      while($products->have_posts()) { 
       $products->the_post(); 
       ?> 

        <div class='content'> 
         <h2><?php the_title(); ?></h2> 
        </div> 
       <?php 
      } 
     } 
     else { 
      echo 'There seems to be a problem, please try searching again or contact customer support!'; 
     } 

謝謝!

+0

+1上一個很好解釋的問題,與相關的代碼,並在您的設置上的細節:)使得它比這裏的一些其他問題更容易回答! –

回答

2

你很近!

在您的tax_query中,taxonomy需要引用'beachevents',而terms需要引用'strand-spellen'。

所以,你的代碼看起來就像這樣:

'tax_query' => array(
      array(
       'taxonomy' => 'thema', 
       'field' => 'slug', 
       'terms' => 'strand-spellen' 
      ) 
     ) 

欲瞭解更多有關建立查詢,你會發現WP_Query documentation有用的 - 有關於分類查詢,在那裏的部分。

+0

謝謝你,你的代碼工作。我確實接近。感謝+1並對我的問題發表評論。 現在我只需要按字母順序對它們進行排序,但這應該是一個問題。 – Steggie

+0

查找WP_Query文檔中的'order'和'orderby'參數:)如果答案幫助了你,請接受它:) –

1

感謝蒂姆的幫助。這是我遇到同樣問題的人的完整代碼。

<?php $args = array(
    'post_type' => 'beachevents', 
    'posts_per_page'=> -1, 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'tax_query' => array(
     array(
     'taxonomy' => 'thema', 
     'field' => 'slug', 
     'terms' => 'strand-spellen' 
       ) 
      ) 
     ); 

     $products = new WP_Query($args); 
      if($products->have_posts()) { 
       while($products->have_posts()) { 
        $products->the_post(); 
?> 

<div class='content'> 
<h2><?php the_title(); ?></h2> 
</div> 
<?php 
    } 
     } 
      else { 
       echo 'There seems to be a problem, please try searching again or contact customer support!'; 
      } ?> 

包括按標題和ASC排序。希望我把它正確地編碼了......

相關問題