Ed.php中显示了多个自定义帖子类型

时间:2015-07-02 作者:TheTimeRanger

我觉得Wordpress中的自定义帖子类型有点麻烦。

我已经创建了多种帖子类型,它们都很有效,但有一件事困扰着我。

假设我有3种自定义帖子类型。电影、相册和视频游戏,每一个都有一定数量的帖子。

现在,在侧栏菜单i admin中,如果我单击Movies的“All Movies”,它会显示最新的条目,无论是电影、相册还是视频游戏。。

有没有办法将它们分开,所以当我按“所有电影”时,它只显示post类型电影中的帖子?

我可以补充一下smashing\'s guide 创建自定义帖子类型。

下面是我用来注册帖子类型的代码示例:

function post_type_movies() {

    $labels = array(
        \'name\'               => _x( \'Movies\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Movie\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'Movie\' ),
        \'add_new_item\'       => __( \'Add New Movie\' ),
        \'edit_item\'          => __( \'Edit Movie\' ),
        \'new_item\'           => __( \'New Movie\' ),
        \'all_items\'          => __( \'All Movies\' ),
        \'view_item\'          => __( \'View Movie\' ),
        \'search_items\'       => __( \'Search Movies\' ),
        \'not_found\'          => __( \'No Movies found\' ),
        \'not_found_in_trash\' => __( \'No Movies found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Movies Post\',

    );

    $args = array(
        \'labels\'             => $labels,
        \'description\'        => \'\',
        \'public\'             => true,
        \'menu_position\'      => 30,
        \'supports\'           => array(\'title\',\'editor\',\'thumbnail\', \'custom-fields\'),
        \'has_archive\'        => true,
        \'menu_icon\'          => \'dashicons-portfolio\',
        \'post_tags\'          => true,
    );
    register_post_type(\'movie-post\', $args);

}
add_action(\'init\',\'post_type_movies\');
下面是其他比较的帖子类型://post Type-Games

function post_type_games() {

    $labels = array(
        \'name\'               => _x( \'Games\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Game\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'game\' ),
        \'add_new_item\'       => __( \'Add New Game\' ),
        \'edit_item\'          => __( \'Edit Game\' ),
        \'new_item\'           => __( \'New Game\' ),
        \'all_items\'          => __( \'All Games\' ),
        \'view_item\'          => __( \'View Game\' ),
        \'search_items\'       => __( \'Search Games\' ),
        \'not_found\'          => __( \'No Games found\' ),
        \'not_found_in_trash\' => __( \'No Games found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Games\',

    );

    $args = array(
        \'labels\'             => $labels,
        \'description\'        => \'\',
        \'public\'             => true,
        \'menu_position\'      => 40,
        \'supports\'           => array(\'title\',\'editor\',\'thumbnail\', \'custom-fields\'),
        \'has_archive\'        => true,
        \'menu_icon\'          => \'dashicons-welcome-widgets-menus\',
        \'post_tags\'          => true,
    );
    register_post_type(\'game\', $args);

}
add_action(\'init\',\'post_type_games\');
//Post Type-位置

function post_type_locations() {

    $labels = array(
        \'name\'               => _x( \'Locations\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Location\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'book\' ),
        \'add_new_item\'       => __( \'Add New Location\' ),
        \'edit_item\'          => __( \'Edit Location\' ),
        \'new_item\'           => __( \'New Product\' ),
        \'all_items\'          => __( \'All Locations\' ),
        \'view_item\'          => __( \'View Location\' ),
        \'search_items\'       => __( \'Search Locations\' ),
        \'not_found\'          => __( \'No Locations found\' ),
        \'not_found_in_trash\' => __( \'No Locations found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Locations\',

    );

    $args = array(
        \'labels\'             => $labels,
        \'description\'        => \'\',
        \'public\'             => true,
        \'menu_position\'      => 30,
        \'supports\'           => array(\'title\',\'editor\',\'thumbnail\', \'custom-fields\'),
        \'has_archive\'        => true,
        \'menu_icon\'          => \'dashicons-location\',
        \'post_tags\'          => true,
    );
    register_post_type(\'location\', $args);

}
add_action(\'init\',\'post_type_locations\');

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

问题是pre_get_posts 在前端和管理端都可以运行,除非您明确告诉它不要运行,以下是您提供的功能,其中有一个小改动:

function pregp_archive_ppp_wpse_108225( $qry ) {
    if( is_admin() ) {
        return;
    }

    if( $qry->is_main_query() && $qry->is_archive() ) {
        $qry->set( \'posts_per_page\', 5 );
        $qry->set( \'post_type\', array( \'post\', \'location\', \'game\' ) );
    }
}
add_action( \'pre_get_posts\', \'pregp_archive_ppp_wpse_108225\' );
注意第一个条件is_admin() - 这将针对任何/每个管理页面进行测试,以便以下更改仅适用于前端查询。

结束

相关推荐