Wordpress - Page and custom post type with this sa

2019-08-23 05:41发布

I have custom post type with "events" slug and page with this same slug ('/events'). When I use this same slug for page and CPT then I have 404 error - pagination. I changed "with_front" to true/false but this not work. I do not know what is wrong.

My code:

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 );

1条回答
倾城 Initia
2楼-- · 2019-08-23 06:11

You cannot have the same slug or name for page and post type. so either you have to disable the archive page of the post by using this:

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

);

OR you have to change either the post name/slug or page name/slug

查看更多
登录 后发表回答