需要打印按当前分类法页面过滤的帖子?

时间:2019-09-26 17:50:48

标签: wordpress custom-wordpress-pages custom-taxonomy

我需要打印按当前分类法页面过滤的所有帖子。 我可以使用哪个查询来针对当前页面分类法过滤所有这些帖子? 我正在尝试做一个动态的小部件,以用于更多具有不同类别的页面。 我希望有一个人可以帮助我 。谢谢 :) 我正在尝试使用此代码,但不起作用...

 <?php
$args=array(
  'post_type' => 'post,
  'post_status' => 'publish',
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

1 个答案:

答案 0 :(得分:0)

已解决:

<?php

 $term_slug = get_query_var( 'term' );
 $taxonomyName = get_query_var( 'taxonomy' );

  $the_query = new WP_Query( array(
    'post_type' => 'post',
    'tax_query' => array(
        array (
            'taxonomy' => $taxonomyName,
            'field' => 'slug',
            'terms' => $term_slug,
          )
        ),
      ));
  ?>