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

时间:2017-12-26 18:01:02

标签: wordpress taxonomy slug

我的自定义帖子类型包含“events”slug和带有相同slug('/ events')的页面。 当我使用同一个slug页面和CPT然后我有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 );

1 个答案:

答案 0 :(得分:1)

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

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

);

或者您必须更改帖子名称/ slug或页面名称/ slug