关于分类查询的问题

时间:2015-05-24 作者:inverted_index

我有一个CPT(主页)和这个CPT的分类法,它有一些字段(关于,服务,…)我想在我的主页中显示每个类别(自定义分类法)的帖子(和内容)。但我不知道怎么做!你能帮帮我吗?任何帮助都将不胜感激。

在函数中。php:

////////////////////////////////
// CPT - Home Page Content
////////////////////////////////

function cpt_homepage(){
    $labels = array(
        \'name\'                => \'Home Page Contents\',
        \'singular_name\'       => \'Home Page Content\',
        \'menu_name\'           => \'Home Page Contents\'
        \'parent_item_colon\'   =>  \'Parent Home Page Content\',
        \'all_items\'           => \'All Home Page Contents\',
        \'view_item\'           => \'View Home Page Content\',
        \'add_new_item\'        =>\'Add New Home Page Content\',
        \'add_new\'             => \'Add New\', 
        \'edit_item\'           =>  \'Edit Home Page Content\',
        \'update_item\'         => \'Update Home Page Content\',
        \'search_items\'        =>  \'Search Home Page Contents\', 
        \'not_found\'           =>  \'Not found\',
        \'not_found_in_trash\'  =>\'Not found in Trash\',
    );
    $args = array(
        \'label\'               =>\'Home Page Contents\',
        \'description\'         => \'Different Home Page Content, our team may design and develop\',
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'thumbnail\' ),
        \'taxonomies\'          => array( \'post_tag\' ),
        \'hierarchical\'        => false,
        \'public\'              => false,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 2,
        \'menu_icon\'           => \'dashicons-welcome-view-site\',
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'post\',
    );
    register_post_type( \'homepage\', $args );
}
add_action( \'init\', \'cpt_homepage\', 0 );
在家里。php(我想查询分类法)

1 个回复
SO网友:Emetrop

This should works.

$args = array(
    \'taxonomy\' => \'your_taxonmy_name\', // replace with your taxonomy name
);

$categories = get_categories( $args );

$ids = array(); 

// lets get all post IDs
foreach ( $categories as $category ) {
    $args = array(
        \'cat\' => $category->term_id,
        \'post_type\' => \'homepage\', // your post type
        \'posts_per_page\' => \'1\',
        \'post__not_in\' => $ids, // don\'t want to any duplication
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        $query->the_post();

        $ids[] = get_the_ID();
    }
}
wp_reset_postdata();

$args = array(
    \'post__in\' => $ids,
);

$query = new WP_Query( $args );

// your loop for echoing
if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();

        // do what you want to
    }
}
wp_reset_postdata();
结束

相关推荐

Custom Post type as Taxonomy

我有一个自定义帖子类型“问题”,并与自定义帖子类型“文章”有关系。我想要的是,创建问题cpt作为文章cpt的分类法,并在文章post列表中显示它。当用户添加新问题时,分类法将自行更新。最终的结果是,我只能根据问题名称过滤文章。有人能帮忙吗?