显示分类法类别中来自特定id或slug的所有帖子


Show all post in taxonomy category from certain id or slug

我有一个自定义的帖子类型叫'electronics',分类类别叫'computer, phone, notebook…和更多的'

在taxonomy.php模板的

中,我成功地使用这段代码获得了分类法名称、段码和id。

<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
echo $term->name; // will show the name
echo $term->slug; // will show the slug
echo $term->term_id; // will show the id
?>

所以如果我在计算机分类页面,我得到'Computers computers 11'

使用每次生成的值中的任何一个

,我如何根据他所在的"分类法页面"生成帖子?如。在计算机分类页面中标记为"计算机"的帖子,在电话分类页面中标记为电话的帖子…等等。

像这样的东西,但是对于分类法…

<?php
$catquery = new WP_Query( 'cat=3' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>

您需要使用tax_query来获取具有特定分类法术语的文章,方法如下:

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'electronics',
            'field' => 'slug',
            'terms' => $term->slug
        )
    )
);
$catquery = get_posts( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>