你能有两个永久链接结构相同的CPT吗?

时间:2017-12-05 作者:chris_huh

我有两个事件自定义帖子类型,我想在相同的永久链接结构下显示,例如:example。com/事件/4560/事件

这可能吗?我曾尝试自己编写代码并使用自定义Post-Type Permalinks插件,但两个CPT中的一个总是返回404错误。

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

注册具有相同permalink结构的两个post类型时,会将它们添加到具有相同索引的重写规则数组中。如果要将两个索引相同的元素添加到php数组中,第一个元素将被覆盖。您的重写将只适用于一种帖子类型,即第二种。

要显示第一种帖子类型的帖子,您必须告诉WordPress要选择哪种帖子类型。要找到它,您必须从这两种职位类型中查询职位数据库,并检查当前职位类型具有哪种职位类型。

我希望下面的代码可以帮助您理解发生了什么。

/**
 * Register event 1 post type
 *
 * Method is used by init hook
 */
function wpse_287785_register_event_1_post_type() {

    $labels = array(
        \'name\' => __( \'Events\' ),
        \'singular_name\' => __( \'Event\' ),
        \'add_new\' => __( \'Add new\' ),
        \'add_new_item\' => __( \'Add new\' ),
        \'edit_item\' => __( \'Edit\' ),
        \'new_item\' => __( \'New\' ),
        \'view_item\' => __( \'View\' ),
        \'search_items\' => __( \'Search\' ),
        \'not_found\' => __( \'Not found\' ),
        \'not_found_in_trash\' => __( \'Not found Events in trash\' ),
        \'parent_item_colon\' => __( \'Parent\' ),
        \'menu_name\' => __( \'Events\' ),

    );

    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'supports\' => array( \'title\', \'page-attributes\' ),
        \'taxonomies\' => array(),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'show_in_nav_menus\' => false,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => array(\'slug\' => \'event\'),
        \'capability_type\' => \'post\',
    );

    register_post_type( \'event_1\', $args );
}

add_action( \'init\', \'wpse_287785_register_event_1_post_type\' );

/**
 * Register event 2 post type
 *
 * Method is used by init hook
 */
function wpse_287785_register_event_2_post_type() {

    $labels = array(
        \'name\' => __( \'Events\' ),
        \'singular_name\' => __( \'Event\' ),
        \'add_new\' => __( \'Add new\' ),
        \'add_new_item\' => __( \'Add new\' ),
        \'edit_item\' => __( \'Edit\' ),
        \'new_item\' => __( \'New\' ),
        \'view_item\' => __( \'View\' ),
        \'search_items\' => __( \'Search\' ),
        \'not_found\' => __( \'Not found\' ),
        \'not_found_in_trash\' => __( \'Not found Events in trash\' ),
        \'parent_item_colon\' => __( \'Parent\' ),
        \'menu_name\' => __( \'Events\' ),

    );

    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'supports\' => array( \'title\', \'page-attributes\' ),
        \'taxonomies\' => array(),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'show_in_nav_menus\' => false,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => array(\'slug\' => \'event\'),
        \'capability_type\' => \'post\',
    );

    register_post_type( \'event_2\', $args );
}

add_action( \'init\', \'wpse_287785_register_event_2_post_type\' );

/**
 * Check if post exits as event_1 post type or as event_2 post type.
 */
function wpse_287785_parse_request($query_vars) {

    if( !is_admin() ) { // Do not parse request on admin.

        $slug = ( isset( $query_vars[\'name\'] ) && !empty( $query_vars[\'name\'] ) ) ? $query_vars[\'name\'] : false;

        if($slug) {

            // Check if post exits as event_1 or event_2 post type
            $query = new WP_Query(array(
                \'post_type\' => array(\'event_1\', \'event_2\'),
                \'name\' => $slug,
            ));

            if( $query->have_posts() ) {

                $post_type = $query->post->post_type;

                // Overwrite query vars for current post type
                $query_vars = array(
                    \'page\' => \'\',
                    $post_type => $slug,
                    \'post_type\' => $post_type,
                    \'name\' => $slug,
                );

                return $query_vars;
            }
        }
    }

    return $query_vars;
}

add_filter( \'request\', \'wpse_287785_parse_request\' );

结束

相关推荐

How to replace permalinks

我已将wordpress项目导出并导入live server。现在的问题是,我将permalink结构作为“localhost”格式。当我单击网站中的任何链接时,它会将我重定向到localhost。我怎样才能改变这一点?我的htaccess文件如下所示<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /myproject/ RewriteRule ^index\\.php$ - [L] RewriteCo