从 WordPress 中的自定义分类中获取所有帖子

发布于 2024-09-11 18:41:11 字数 361 浏览 10 评论 0原文

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

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

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

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

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

Is there a way to get all the posts from a taxonomy in Wordpress ?

In taxonomy.php, I have this code that gets the posts from the term related to the current term.

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

I'd like to create a page with all the posts in the taxonomy, regardless of the term.

Is there a simple way to do this, or do I have to query the taxonomy for the terms, then loop trough them, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谁许谁一生繁华 2024-09-18 18:41:11

@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

@PaBLoX made a very nice solution but I made a solution myself what is little tricky and doesn't need to query for all the posts every time for each of the terms. and what if there are more than one term assigned in a single post? Won't it render same post multiple times?

 <?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

$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;

我发布了一些非常相似的内容此处

$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');    
echo  $myterms[0]->name;

With that you'd post the first item, yo can then create a foreach; loop:

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

That way you'd list them, if you want to post all of them, -my solution- create a normal wordpress loop inside the foreach one, but it has to have something like:

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;

I posted something very similar here.

最冷一天 2024-09-18 18:41:11

与帖子类型不同,WordPress 没有用于分类法块本身的路径。

要使分类法本身列出分配有任何分类法术语的所有帖子,您需要使用 WP_Querytax_queryEXISTS 运算符:

// 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;
  }
}

Unlike for post types, WordPress does not have a route for the taxonomy slug itself.

To make the taxonomy slug itself list all posts that have any term of the taxonomy assigned, you need to use the EXISTS operator of tax_query in WP_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;
  }
}
活雷疯 2024-09-18 18:41:11
<?php
get_posts(array(
    'post_type' => 'gallery',
    'tax_query' => array(
        array(
        'taxonomy' => 'gallery_cat',
        'field' => 'term_id',
        'terms' => 45)
    ))
);
?>
<?php
get_posts(array(
    'post_type' => 'gallery',
    'tax_query' => array(
        array(
        'taxonomy' => 'gallery_cat',
        'field' => 'term_id',
        'terms' => 45)
    ))
);
?>
韬韬不绝 2024-09-18 18:41:11

在术语的查询循环中,您可以收集数组中的所有帖子引用,并稍后在新的 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 );

While in the query loop for terms, you can collect all post references in an array and use that later in a new 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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文