【问题标题】:WordPress get posts by category_name is not workingWordPress 按类别名称获取帖子不起作用
【发布时间】:2019-04-11 14:40:49
【问题描述】:

我需要按类别列出帖子。我通过按类别显示帖子遇到问题没有人为我工作,例如“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; 
}

【问题讨论】:

  • “不工作”是什么意思?会发生什么?

标签: php wordpress loops


【解决方案1】:

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

$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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2021-07-25
    • 2015-11-09
    相关资源
    最近更新 更多