【问题标题】:Get all posts from custom taxonomy in Wordpress从 Wordpress 中的自定义分类中获取所有帖子
【发布时间】:2010-07-28 15:10:23
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: php wordpress taxonomy


    【解决方案1】:

    @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

    【讨论】:

    • 这看起来更正确,因为 "tax_query" 中的 "terms" 接受数组。
    【解决方案2】:
    $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;
    

    我发布了非常相似的here

    【讨论】:

    • 上面'taxonomy' =&gt; '$term_name'行的例子需要像'taxonomy' =&gt; "$term_name"这样双引号,或者最好不要像'taxonomy' =&gt; $term_name这样的引号,或者甚至更好地省略之前的赋值而只使用'taxonomy' =&gt; $term-&gt;slug。也就是说,使用'tax_query' =&gt; array(...) 显示has been deprecated in favor of 的方法。希望这会有所帮助。
    • 抱歉,耽搁了……你说得对。我相应地修改了我的答案:)
    • 太棒了!我希望我们的努力能帮助其他人。
    • 我不太清楚为什么,但为了让这个 sn-p 工作,我不得不从 foreach 循环中删除 WordPress 循环部分。似乎每次foreach 迭代时,它都会创建一个重复的帖子部分。举个例子,如果我在三个不同的术语下有三个帖子,并且我想将它们全部显示在一个页面上,并且我使用了开箱即用的这个 sn-p,它会按原样显示这三个帖子,但它会然后根据我的分类学中的术语数量,在第一个帖子之后再复制三个帖子两次。@pablox @MikeSchinkel
    • 好吧,这是我大约 5 年前编造的。我几乎不记得这件事了,而且,Wordpress 已经遇到了很多 API 更改和主要版本更新......抱歉没有提供更多帮助 =(
    【解决方案3】:

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

    要使分类 slug 本身列出所有分配有任何分类术语的帖子,您需要使用 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;
      }
    }
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2012-04-01
        • 2016-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-15
        • 1970-01-01
        • 2018-06-29
        • 1970-01-01
        相关资源
        最近更新 更多