在soronomy.php中显示按帖子类型分组的帖子

时间:2016-03-14 作者:vj tester

我有两种与自定义分类法(newcategory)(writer1、writer2、writer3…)关联的帖子类型(“newpost”和“book”)

我需要显示每一个单独的风格与后期定制职位。

我的代码是:

 $post_type = array(\'newpost\',\'book\');
 //echo get_query_var(\'term\');

     foreach($post_type as $postType)
     {
 $args = array(
    \'post_type\' => $postType,
    \'tax_query\' => array(
                array(
                    \'taxonomy\' => $taxonomy,
                    \'field\' => \'slug\',
                    \'terms\' => rawurldecode(get_query_var(\'term\'))
                )
            )
    );

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

//I got all the post here by //echo \'<h1>\'.the_title().\'</h1>\'; but i have need it separately.          
//Here I want to display all the term of the custom post separately  

      endwhile;
 endif;


     }

1 个回复
SO网友:iantsch

循环调用中的简短版本:get_template_part( \'loop\', get_post_type() );

你现在需要loop.php 作为常规回退模板(默认视图)。您可以根据需要生成任意多个post\\u类型特定的模板,例如:。loop-newpost.phploop-book.php.

但是您想得有点过头了,在主查询的顶部添加了另一个查询(页面加载?)。您可以使用以下内容向主题添加完整的模板支持。

更好的方法

Add your CPTs.

function add_my_custom_post_type() {
    $args = array(
        \'menu_position\' => 8,
        \'menu_icon\' => \'dashicons-admin-post\',
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'query_var\' => false,
        \'capability_type\' => \'post\',
        \'has_archive\' => true,
        \'rewrite\' => array(
            \'slug\' => \'new-post\',
            \'feeds\' => true,
            \'pages\' => true,
            \'with_front\' => true,
        ),
        \'taxonomies\' => array(
            \'author\'
        ),
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'thumbnail\',
            \'excerpt\'
        )
    );
    register_post_type( \'newpost\', $args );
    $args[\'labels\'][\'name\'] = __(\'Books\', \'my_textdomain\');
    $args[\'labels\'][\'all_items\'] = __(\'All Books\', \'my_textdomain\');
    $args[\'labels\'][\'singular_name\'] = __(\'Book\', \'my_textdomain\');
    $args[\'rewrite\'][\'slug\'] = \'book\';
    register_post_type( \'book\', $args );
}
add_action(\'init\', \'add_my_custom_post_type\');
Add your custom taxonomy.

Attention: 作者是WordPress使用的鼻涕虫,所以你需要有创造力:tax_author 分类名称(&A);query_varthe-author 对于鼻涕虫。

function add_my_custom_taxonomy() {
    register_taxonomy(
        \'tax_author\',
        array(
            \'newpost\',
            \'book\'
        ),
        array(
            \'hierarchical\' => false,
            \'labels\' => array(
                \'name\' => __( \'Authors\', \'my_textdomain\' ),
                \'singular_name\' => __( \'Author\', \'my_textdomain\' ),
                \'menu_name\' => __( \'Author\', \'my_textdomain\' ),
            ),
            \'show_ui\' => true,
            \'show_admin_column\' => true,
            \'update_count_callback\' => \'_update_post_term_count\',
            \'query_var\' => true,
            \'rewrite\' => array(
                \'slug\' => \'the-author\',
                \'with_front\' => true,
                \'hierarchical\' => false
            )
        )
    );
}
add_action(\'init\', \'add_my_custom_taxonomy\');
Add the CPT to the main queries 在与pre_get_posts

function add_my_custom_post_type_to_query( $query ) {
    if ( $query->is_main_query() && $query->is_home() && !is_admin() /* No filtering of CPTs in the backend */) {
        $query->set( \'post_type\', array(\'newpost\', \'book\') );
    }
}

add_action( \'pre_get_posts\', \'add_my_custom_post_type_to_query\' );
Edit: 分类法不需要这个,但如果要将CPT添加到主查询,请使用这个钩子。-感谢@Pieter Goosen的澄清

在您的taxonomy-tax_author.php 模板文件,您现在可以use the main loop:

if (have_posts()) while (have_posts()):
    the_post();
    get_template_part( \'loop\', get_post_type() );
你现在需要loop.php 作为常规回退模板(默认视图)。您可以根据需要生成任意多个post\\u类型特定的模板,例如:。loop-newpost.phploop-book.php.

Update:如果要按post\\u类型排序查询,则需要连接到pre_get_posts 再一次

function order_my_custom_taxonomy( $query ) {
    if ( $query->is_main_query() && $query->is_tax(\'tax_author\') && !is_admin() /* No filtering of CPTs in the backend */) {
        $query->set( \'orderby\', \'type\' );
        $query->set( \'order\', \'ASC\' );
    }
}

add_action( \'pre_get_posts\', \'add_my_custom_post_type_to_query\' );
有关如何筛选和订购的详细信息,请访问提供的链接。

有关以下内容的详细信息:

相关推荐