Wordpress - 页面和自定义帖子类型具有相同的 slug [英] Wordpress - Page and custom post type with this same slug

查看:37
本文介绍了Wordpress - 页面和自定义帖子类型具有相同的 slug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有事件"slug 的自定义帖子类型和具有相同 slug ('/events') 的页面.当我对页面和 CPT 使用相同的 slug 时,我会遇到 404 错误 - 分页.我将with_front"更改为真/假,但这不起作用.我不知道出了什么问题.

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.

我的代码:

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

推荐答案

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

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,
    [...]
)

);

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

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

这篇关于Wordpress - 页面和自定义帖子类型具有相同的 slug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆