首页 > 解决方案 > WordPress 按类别名称获取帖子不起作用

问题描述

我需要按类别列出帖子顺序。我通过按类别显示帖子遇到问题没有人为我工作,例如“Category_name、tag_id、cat 等。”

$args1 = array(
    'taxonomy'=> 'portfolio_cat',
    'orderby' => 'slug',
    'order'   => 'ASC',
);

$cats = get_categories($args1);
foreach ($cats as $cat) 
{
  //echo $cat->slug;
  $args2 = array(
  'post_type' => 'advanced_portfolio',
  'posts_per_page' => -1,
   //'tag_id' => 25,
   // 'category_name' =>$cat->slug,
   'category_name' =>'video' //my category name **slug**
   );

   query_posts($args2);
   if (have_posts()) : 
   while (have_posts()) : the_post(); 
   the_title();
   endwhile;

   endif; 
}

标签: phpwordpressloops

解决方案


您正在使用自定义帖子类型,这意味着它不会有常规类别,而是会有一个用作类别的分类法,这意味着您需要使用税务查询,还需要检查条款,而不是经过测试但应该可以工作:

$taxonomy = 'portfolio_cat';
$cats = get_terms($taxonomy);

foreach ($cats as $cat) {
  $args1 = array(
    'post_type' => 'advanced_portfolio',
    'posts_per_page' => -1,
    'tax_query' => array(
      array(
        'taxonomy' => 'portfolio_cat',
        'field'    => 'slug',
        'terms'    => 'video'
      ),
    ),
  );

  query_posts($args1);

  if (have_posts()) : 
    while (have_posts()) : the_post(); 
      the_title();
    endwhile;
  endif;
}
wp_reset_query();

推荐阅读