在循环中显示自定义分类Wordpress

问题描述:

好的,这可能是一个简单的。但我似乎无法找出出于某种原因。在循环中显示自定义分类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上一个很好解释的问题,与相关的代码,并在您的设置上的细节:)使得它比这里的一些其他问题更容易回答! –

你很近!

在您的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'参数:)如果答案帮助了你,请接受它:) –

感谢蒂姆的帮助。这是我遇到同样问题的人的完整代码。

<?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排序。希望我把它正确地编码了......