Get all posts from categories

时间:2016-09-19 作者:mysticalghoul

我正在尝试打印所有类别的所有帖子,但此时要在类别内显示的帖子将打印所有类别下的所有帖子。这是代码。您可以使用快捷码[bpo\\U microsoft\\U快捷码]。提前感谢:

        <?php
        function Custom_Post_Type_Grouped_By_Category(){ 
            global $post;
            $cat_args = array(
                \'orderby\'       => \'term_id\', 
                \'order\'         => \'ASC\',
                \'hide_empty\'    => true, 
            );    
            $terms = get_terms(\'Custom Post Type Reg\', $cat_args);
            $content = \' \';
            $content .= \'<ul >\';
                foreach($terms as $taxonomy){
                    $term_slug = $taxonomy->slug;
                    $cat_id = $taxonomy->term_id;
                    $tax_post_args = array(
                          \'post_type\' => \'Custom Post Type\',
                          \'posts_per_page\' => 999,
                          \'order\' => \'ASC\',
                          \'numberposts\' => -1,
                          \'tax_query\' => array(
                                array(
                                     \'taxonomy\' => \'Custom Post Type Reg\',
                                     \'field\' => \'slug\',
                                     \'terms\' => $term_slug,
                                     \'id\'    => $cat_id
                                ),
                           )
                    );    
                      $content .= \'<li >\'; 
                        $content .= \'<div >\';         
                            $content .= $taxonomy->name;
                                $the_query = null;
                                $the_query = new WP_Query( $tax_post_args );  
                                if ( $the_query->have_posts() ) {                           
                                    $content .= \'<ul >\';
                                    while ( $the_query->have_posts() ) {
                                        $the_query->the_post();
                                        $content .= \'<li><a href="\' . get_the_permalink() .\'" rel="bookmark">\' . get_the_title() .\'</a></li>\';
                                    }                     
                                    $content .= \'</ul>\';
                                }             
                                wp_reset_postdata();       
                         $content .= \'</div>\';    
                      $content .= \'</li>\';           
                    } //end foreach loop
            $content .= \'</ul>\';
            return $content;

        }
        add_shortcode(\'shortcode\', \'Custom_Post_Type_Grouped_By_Category\');
        ?>

1 个回复
最合适的回答,由SO网友:Aniruddha Gawade 整理而成

从中,您似乎希望按类别对所有产品进行分组。您的查询似乎是正确的,除了$term_slug 不需要在引号中。

$tax_post_args = array(
                  \'post_type\' => \'bpo_microsoftoffice\',
                  \'posts_per_page\' => 999,
                  \'order\' => \'ASC\',
                  \'tax_query\' => array(
                        array(
                             \'taxonomy\' => \'bpo_microsoftoffice_reg\',
                             \'field\' => \'slug\',
                             \'terms\' => $term_slug
                        )
                   )
            ); 
编辑:

方法1:

如果只需要编写的查询,请使用WP_Query($tax_post_args );.

方法2:

如果你两者都想要categorycustom_taxonomy, 添加\'category\' => $cat_id, 在里面$tax_post_args.

所以

$tax_post_args = array(
              \'post_type\' => \'bpo_microsoftoffice\',
              \'posts_per_page\' => 999,
              \'order\' => \'ASC\',
              \'tax_query\' => array(
                    \'relation\' => \'OR\',
                    array(
                         \'taxonomy\' => \'bpo_microsoftoffice_reg\',
                         \'field\' => \'slug\',
                         \'terms\' => $term_slug
                    ),
                    array(
                         \'taxonomy\' => \'category\',
                         \'field\' => \'slug\',
                         \'terms\' => $cat_id
                    ),
               )
        ); 
以及使用WP\\u查询($tax\\u post\\u args);

这是假设你post_type 正在进行category 以及bpo_microsoftoffice_reg 作为分类法。要是…就好了bpo_microsoftoffice_reg 如果已指定,请使用第一种方法。