Wordpress - 页面和自定义 post 类型具有相同的 slug

Wordpress - Page and custom post type with this same slug

我有自定义 post 类型和 "events" slug 和页面有相同的 slug ('/events')。 当我对页面和 CPT 使用相同的 slug 时,我会出现 404 错误 - 分页。 我将 "with_front" 更改为 true/false 但这不起作用。 不知道怎么回事。

我的代码:

function mr_cpt_event() {

    $labels = array(
        'name'                  => _x( 'Event', 'Post Type General Name', 'mr' ),
        'singular_name'         => _x( 'Event', 'Post Type Singular Name', 'mr' ),
        'menu_name'             => __( 'Events', 'mr' ),
        'name_admin_bar'        => __( 'Event Type', 'mr' ),
        'archives'              => __( 'Event Archives', 'mr' ),
        'parent_item_colon'     => __( 'Parent Event', 'mr' ),
        'all_items'             => __( 'All Events', 'mr' ),
        'add_new_item'          => __( 'Add New Event', 'mr' ),
        'add_new'               => __( 'Add New', 'mr' ),
        'new_item'              => __( 'New Event', 'mr' ),
        'edit_item'             => __( 'Edit Event', 'mr' ),
        'update_item'           => __( 'Update Event', 'mr' ),
        'view_item'             => __( 'View Event', 'mr' ),
        'search_items'          => __( 'Search Event', 'mr' ),
        'not_found'             => __( 'Not found', 'mr' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'mr' ),
        'featurmr_image'        => __( 'Featured Image', 'mr' ),
        'set_featurmr_image'    => __( 'Set featured image', 'mr' ),
        'remove_featurmr_image' => __( 'Remove featured image', 'mr' ),
        'use_featurmr_image'    => __( 'Use as featured image', 'mr' ),
        'insert_into_item'      => __( 'Insert into item', 'mr' ),
        'uploadmr_to_this_item' => __( 'Uploaded to this item', 'mr' ),
        'items_list'            => __( 'Items list', 'mr' ),
        'items_list_navigation' => __( 'Items list navigation', 'mr' ),
        'filter_items_list'     => __( 'Filter items list', 'mr' )
    );
    $rewrite = array(
        'slug'                  => 'events',
        'with_front'            => false,
        'pages'                 => true,
        'feeds'                 => true
    );
    $args = array(
        'label'                 => __( 'Event', 'mr' ),
        'description'           => __( 'Events', 'mr' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', ),
        'taxonomies'            => array( 'event_category', 'event_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-laptop',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'rewrite'               => $rewrite,
        'capability_type'       => 'post'
    );
    register_post_type( 'event', $args );

}
add_action( 'init', 'mr_cpt_event', 0 );

页面和 post 类型不能有相同的 slug 或名称。 所以要么你必须使用这个禁用 post 的存档页面:

register_post_type( 'events',
array(
    [...]
    'has_archive' => false,
    [...]
)

);

或者您必须更改 post name/slug 或页面 name/slug