首页 > 解决方案 > 需要打印由我当前的分类页面过滤的帖子吗?

问题描述

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

 <?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().
?>

标签: wordpresscustom-wordpress-pagescustom-taxonomy

解决方案


解决了:

<?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,
          )
        ),
      ));
  ?>

推荐阅读