自定义Post Type Permalink路径异常,with_front=false不生效求助
自定义Post Type被强制添加/blog前缀的解决思路
问题描述
我将WordPress通用文章的Permalink设置为/blog/%postname%/,但该设置导致自定义Post Type也被自动加上了/blog前缀。例如我的help类型原本应该使用/help/%postname%/这类自定义路径,现在却变成了blog/help/%postname%/。我尝试给help类型设置with_front=false但并未生效,求可行的解决办法。
用户实现代码
$slugs = [ 'help' => apply_filters( 'amzshyar_help_slug', 'help' ), 'category' => apply_filters( 'amzshyar_help_category_slug', 'help-category' ) ]; register_post_type( $slugs[ 'help' ], array( 'label' => _tstr( 'پایگاه دانش' ), 'description' => _tstr( 'پایگاه دانش' ), 'labels' => array( 'name' => _tstr( 'پایگاه دانش' ), 'singular_name' => _tstr( 'پایگاه دانش' ), 'menu_name' => _tstr( 'پایگاه دانش' ), 'parent_item_colon' => _tstr( 'پایگاه دانش' ), 'all_items' => 'پایگاه دانش', 'view_item' => 'مشاهده مطلب', 'add_new_item' => 'افزودن مطلب جدید', 'add_new' => 'افزودن مطلب جدید', 'edit_item' => 'ویرایش مطلب', 'update_item' => 'بروزرسانی', 'search_items' => 'جستجو', 'not_found' => 'موردی یافت نشد.', 'not_found_in_trash' => 'موردی یافت نشد.' ), 'menu_icon' => 'dashicons-editor-help', 'supports' => array( 'title', 'editor', 'excerpt' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 5, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'page', 'show_in_rest' => true, 'taxonomies' => array( $slugs[ 'category' ] ), ) ); register_taxonomy( $slugs[ 'category' ], array( $slugs[ 'help' ] ), array( 'hierarchical' => true, 'label' => 'دسته', 'singular_label' => 'دسته', 'rewrite' => array( 'slug' => $slugs[ 'category' ], 'with_front'=> false ), 'show_admin_column' => true, 'show_ui' => true, 'show_in_rest' => true, 'query_var' => true ) );
解决步骤
补全register_post_type的rewrite配置
你的代码中注册自定义Post Type时,完全缺少rewrite参数配置。WordPress默认会将全局Permalink的前缀(即/blog/)附加到自定义Post Type路径前,必须显式添加rewrite数组并设置with_front=false才能跳过这个前缀。修改后的
register_post_type参数需添加:'rewrite' => array( 'slug' => $slugs['help'], 'with_front' => false ),刷新Permalink规则
修改代码后,务必进入WordPress后台「设置→固定链接」页面,直接点击「保存更改」(无需修改任何设置),让系统重新生成rewrite规则。这一步是配置生效的关键,很多时候配置无效都是因为没刷新规则。验证分类法配置
你已给自定义分类法设置with_front=false,这个配置是正确的,只需确保分类法的slug与Post Type的slug无冲突,避免路径解析错误。
内容的提问来源于stack exchange,提问作者Sepehr Zamani
相关产品推荐
相关产品推荐

