简体   繁体   English

WordPress按category_name获取帖子不起作用

[英]WordPress get posts by category_name is not working

I need to List Posts order by category. 我需要按类别列出帖子顺序。 I getting problem by display Posts by category No one is working for me, Like "Category_name, tag_id, cat, etc.." 我的显示出现问题按类别的帖子没有人为我工作,例如“ 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; 
}

You're using a custom post types which means it won't have regular categories but instead it will have a taxonomy which is used as a category, this means you need to use a tax query and also you need to check the terms, not tested but should work: 您使用的是自定义帖子类型,这意味着它不会包含常规类别,但会具有用作类别的分类法,这意味着您需要使用税款查询,还需要检查条款,而不是经过测试,但应该可以工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM