2016-09-16 190 views
0

我有一个自定义帖子类型被称为产品产品类别作为其分类名称。它包含每个都有子类别和帖子的类别。显示帖子数自定义帖子类型的子类别

我想仅显示子类别中所有帖子的计数。

这是我到目前为止有:

<?php 
// POSTS LOOP 

// Query 
$args = array('post_type' => 'products' , 'orderby' => 'menu_order', 'order' => 'ASC' , 
    'tax_query' => array(
     array(
      'taxonomy' => 'product_categories', 
      'terms' => array(14), 
      'include_children' => false 
     ) 
)); 
$query = new WP_Query($args); 

// Post Counter 
$term_id = 14; 
$tax_name = 'product_categories'; 
$terms = get_terms($tax_name, array('parent' => 0)); 
$term_children = get_term_children($term_id,$tax_name); 
$post_count = 0; 

// Loop Structure 
echo '<ul>'; 
while ($query->have_posts()) : $query->the_post(); 
    echo '<li class="zazzoo-page">'; 

     foreach($terms as $term) { 

      $term_children = get_term_children($term->term_id,$tax_name); 
      echo '<ul>'; 
      foreach($term_children as $term_child_id) { 
       $term_child = get_term_by('id',$term_child_id,$tax_name); 
       $post_count = $term_child->count; 
       echo '<div class="zazzoo-counter">'. $post_count .' Designs</div>'; 
      } 
      echo '</ul>'; 

     } ?> 

     <div class="title-border"><div class="page-title"><?php the_title(); ?></div></div> 
     <div class="hovereffect"> 
      <?php the_post_thumbnail('zazzoo-thumb', array('class' => 'responsive-img')); ?> 
      <div class="overlay"> 
       <h2><?php the_title(); ?></h2> 
       <?php the_content(); ?> 
      </div> 
     </div> 
    <?php echo '</li>'; 
endwhile; 
echo '</ul>'; 

wp_reset_query(); 
?> 

Just some visual of what the above code displays 以上是它目前看起来像一个图像。 小册子卡片标签(其中每篇文章有4篇文章)是文章计数值应该显示的子类别,其余文章是帖子。

我还在学习WordPress主题开发,所以如果任何人都可以提供一些指导,将不胜感激。

+0

所以,小册子和卡片标签是子类别,但地板图形,马克杯,记事本和贴纸是帖子? –

+0

是的,这是正确的。所以我只想计算这些子类别中的帖子。 – Ragmah

+0

使用您提供的代码,您似乎输出了6篇文章,并且没有任何类别或子类别(甚至小册子和卡片标签作为文章输出)。 –

回答

1

如果你想要帖子和术语与帖子混合在一起并显示出来 - 这是一个非常艰巨的任务。

我会建议你只使用列表中的一个或另一个 - 更容易。

要到,你可以做到以下几点:

  1. 创建的所有讯息要显示的类别(例如,创建“地板图形”,“杯”,“记事本”和“贴纸“),就像您为”小册子“和”卡片标签“所做的一样。这样你将有2个子类别,其中有4-4个帖子,4个子类别有1-1个帖子。
  2. 如果你这样做,那么你只需要使用get_terms()查询。

  这可能是你的查询(您想看到ID为14项小类):

$tax_name = 'product_categories'; 
$parent_id = 14; 
$terms = get_terms($tax_name, array('parent' => $parent_id, 'pad_counts' => 1,)); 

然后你就可以走了,和打印:

$html = ''; 
$html .= '<ul>'; 
foreach ($terms as $single_term) { 
    $html .= '<li class="zazzoo-page">'; 
    // only put the tag on terms, that have more than 1 items in it 
    if ($single_term->count > 1) { 
     $html .= '<div class="zazzoo-counter">'. $single_term->count .' Designs</div>'; 
    } 
    $html .= '<div class="title-border">'; 
    $html .= '<div class="page-title">'; 
    $html .= $single_term->name; 
    $html .= '</div>'; 
    $html .= '</div>'; 
    $html .= '<div class="hover effect">'; 
    $html .= '<div class="overlay">'; 
    $html .= $single_term->name; 
    $html .= '</div>'; 
    $html .= '</div>'; 
    $html .= '</li>'; 
} 
$html .= '</ul>'; 

echo $html; 

这样你就可以得到名单和计数列表(需要的地方),但是你将失去图像和内容(悬停时)。

如果你想要的形象和内容的时候,你可以查询每个子类别的第一篇文章对这一内容,是这样的:

$posts_array = get_posts(
    array(
     'posts_per_page' => 1, 
     'post_type' => 'products', 
     'tax_query' => array(
      array(
       'taxonomy' => 'product_categories', 
       'field' => 'term_id', 
       'terms' => $single_term->term_id, 
      ) 
     ) 
    ) 
); 

或者你可以指定一个图像和/或描述您显示的自定义分类子类别。

我希望这可以帮助你一点。 :)

+0

这绝对是:) :)感谢一百万穆卡! – Ragmah

+0

很高兴听到它! :d –

相关问题