显示自定义帖子的自定义分类下的术语帖子

时间:2016-11-23 05:34:19

标签: php wordpress custom-taxonomy

我创建了自定义帖子Electronics。然后Electronics有自定义分类appliencesappliances包含home applianceskitchen appliances等字词。现在home appliances有一些帖子。

现在我想在home appliances字词下显示帖子。我创建了一个页面taxonomy-appliances.php

---- Electronics (custom post)
      |__ appliances (custom taxonimy)
          |__home appliances
             |__ posts
             |
          |__Kitchen appliances
             |_posts

我如何在该页面中显示它?

这是我的代码:

<?php

    $args = array(
            'tax_query' => array(
                array(
                    'taxonomy' => 'appliances',
                    'field' => 'slug',
                    'terms' => 'home appliances',
                )
            )
        );
    $posts = new WP_Query($args);

    if($posts->have_posts()){
        while ($posts->have_posts()) : $posts->the_post();

            echo get_the_title();

        endwhile;
    }

?>

但没有显示

2 个答案:

答案 0 :(得分:0)

$args = array(
  'post_type' => 'electronics', // your post type name
  'posts_per_page' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'appliances', //your taxonomy name
      'field' => 'slug',
      'terms' => 'home-appliances',//here use the slug, check the slug of your term

    )
  )
);

答案 1 :(得分:0)

 Try this, 

  $posts_array = get_posts(
    array(
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'appliances',
                'field' => 'slug',
                'terms' => 'home-appliances',
            )
        )
    )
);