Foreach循环在查询Wordpress分类时返回多个项目

Foreach循环在查询Wordpress分类时返回多个项目

问题描述:

我在查询分类术语,然后列出这些术语下的帖子。分类法基本上是一种自定义帖子类型的类别,其中的术语是类别。Foreach循环在查询Wordpress分类时返回多个项目

我不认为这是可能的,但我有一个foreach循环,应该循环查看每个术语(类别),然后列出页面上的术语(类别),然后列出帖子这些条款(类别)下面的问题是,它列出了每个类别下所有类别(条款)的帖子。所以我得到每个职位的倍数。帖子只添加在一个类别下。

我找不出为什么它重复的帖子,直到我偶然尝试获取类别描述的变量值之前甚至声明,当我看到它没有得到第一类的描述值,但它做了第二个,我知道它是一次为单个foreach循环获取多个类别的帖子。这甚至有可能吗?有人能告诉我我的问题是什么,并帮助我纠正这个问题吗?

<?php 

    $myterms = get_terms('menu-category', 'orderby=none&hide_empty'); 
     foreach ($myterms as $term) : ?> 
      <h3><?php echo $term->name; ?></h3> <?php 
       $term_name = $term->slug; 
       $term_desc = $term->description; 
      if($term_desc) 
       { ?> 
       <div class="menu-intro"><p><?php echo $term_desc; ?></p></div> 
     <?php } else { ?> 
       <div class="menu-intro"></div> 
     <?php } 

       $args = array(
       'post_type' => 'restaurant-menu', 
       'taxonomy' => $term_name, 
       ); 

      // assigning variables to the loop 
      global $wp_query; 
      $wp_query = new WP_Query($args); 

      // starting loop posting only 
      while ($wp_query->have_posts()) : $wp_query->the_post(); ?> 

        <ul class="dishes"> 
              <li> 
          <h4 class="menu-item"><?php the_title(); ?></h4> 
          <p class="menu-description"><?php the_content(); ?></p> 

         <?php $price_value = get_field("show_price"); 
         if($price_value == "Yes") 
         { ?> 
         <i class="price">$ <?php $price_value ?></i> 
        <?php } ?> 
         </li> 

     <?php endwhile; 

    endforeach; 
    ?> 

我发现我的问题是,我使用该术语作为分类,它给我所有职位的分类,而不是类别(术语)。

这是一个快速解决,我只希望我会陷入到昨晚:

$args = array(
      'post_type' => 'restaurant-menu', 
      'tax_query' => array(
      array(
      'taxonomy' => 'menu-category', 
      'field' => 'slug', 
      'terms' => $term_name 
      ) 
     ) 
    );