11

有没有办法从 Wordpress 的分类中获取所有帖子?

taxonomy.php中,我有这段代码可以从与当前术语相关的术语中获取帖子。

$current_query = $wp_query->query_vars;
query_posts( array( $current_query['taxonomy'] => $current_query['term'], 'showposts' => 10 ) );

无论术语如何,我想创建一个包含分类中所有帖子的页面。

有没有一种简单的方法可以做到这一点,或者我必须查询术语的分类,然后循环遍历它们,等等。

4

4 回答 4

13

@PaBLoX 提出了一个非常好的解决方案,但我自己提出了一个有点棘手的解决方案,并且不需要每次都为每个术语查询所有帖子。如果在一个帖子中分配了多个术语怎么办?它不会多次渲染同一个帖子吗?

 <?php
     $taxonomy = 'my_taxonomy'; // this is the name of the taxonomy
     $terms = get_terms($taxonomy);
     $args = array(
        'post_type' => 'post',
        'tax_query' => array(
                    array(
                        'taxonomy' => 'updates',
                        'field' => 'slug',
                        'terms' => wp_list_pluck($terms,'slug')
                    )
                )
        );

     $my_query = new WP_Query( $args );
     if($my_query->have_posts()) :
         while ($my_query->have_posts()) : $my_query->the_post();

              // do what you want to do with the queried posts

          endwhile;
     endif;
  ?>

wp_list_pluck

于 2013-05-29T15:13:48.157 回答
12
$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');    
echo  $myterms[0]->name;

这样你就可以发布第一个项目,然后你可以创建一个foreach; 环形:

foreach ($myterms as $term) { ?>
    <li><a href="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></li> <?php
} ?>

这样你就可以列出它们,如果你想发布所有这些,-我的解决方案-在 foreach 中创建一个普通的 wordpress 循环,但它必须有类似的东西:

foreach ($myterms as $term) :

$args = array(
    'tax_query' => array(
        array(
            $term->slug
        )
    )
);

//  assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);

// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();

the_title();
blabla....

endwhile;

endforeach;

我在这里发布了一些非常相似的东西。

于 2010-09-27T06:09:10.980 回答
2

与帖子类型不同,WordPress 没有分类 slug 本身的路径。

要使分类 slug 本身列出所有分配有任何分类术语的帖子,您需要使用 in 的运算EXISTStax_queryWP_Query

// Register a taxonomy 'location' with slug '/location'.
register_taxonomy('location', ['post'], [
  'labels' => [
    'name' => _x('Locations', 'taxonomy', 'mydomain'),
    'singular_name' => _x('Location', 'taxonomy', 'mydomain'),
    'add_new_item' => _x('Add New Location', 'taxonomy', 'mydomain'),
  ],
  'public' => TRUE,
  'query_var' => TRUE,
  'rewrite' => [
    'slug' => 'location',
  ],
]);

// Register the path '/location' as a known route.
add_rewrite_rule('^location/?$', 'index.php?taxonomy=location', 'top');

// Use the EXISTS operator to find all posts that are
// associated with any term of the taxonomy.
add_action('pre_get_posts', 'pre_get_posts');
function pre_get_posts(\WP_Query $query) {
  if (is_admin()) {
    return;
  }
  if ($query->is_main_query() && $query->query === ['taxonomy' => 'location']) {
    $query->set('tax_query', [
      [   
        'taxonomy' => 'location',
        'operator' => 'EXISTS',
      ],  
    ]);
    // Announce this custom route as a taxonomy listing page
    // to the theme layer.
    $query->is_front_page = FALSE;
    $query->is_home = FALSE;
    $query->is_tax = TRUE;
    $query->is_archive = TRUE;
  }
}
于 2017-06-16T12:56:25.153 回答
0

在术语的查询循环中,您可以将所有帖子引用收集到一个数组中,然后在新的 WP_Query 中使用它。

$post__in = array(); 
while ( $terms_query->have_posts() ) : $terms_query->the_post();
    // Collect posts by reference for each term
    $post__in[] = get_the_ID();
endwhile;

...

$args = array();
$args['post__in'] = $post__in;
$args['orderby'] = 'post__in';
$other_query = new WP_Query( $args );
于 2015-07-31T13:05:29.233 回答