You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义分类法与WooCommerce冲突,分类链接404错误如何解决?

问题描述

我开发了一款自定义插件,仅用于注册自定义文章类型(dl)、其分类法(downloads)及标签式分类法(tagged),代码如下:

function dl_setup_post_types() {

    $dl_labels =  apply_filters( 'dl_labels', array(
        'name'                => 'Files',
        'singular_name'       => 'File',
        'add_new'             => __('Add New', 'dl'),
        'add_new_item'        => __('Add New File', 'dl'),
        'edit_item'           => __('Edit File', 'dl'),
        'new_item'            => __('New File', 'dl'),
        'all_items'           => __('All Files', 'dl'),
        'view_item'           => __('View File', 'dl'),
        'search_items'        => __('Search Files', 'dl'),
        'not_found'           => __('No Files found', 'dl'),
        'not_found_in_trash'  => __('No Files found in Trash', 'dl'),
        'parent_item_colon'   => '',
        'menu_name'           => __('Files', 'dl'),
        'exclude_from_search' => true,
        
    ) );


    $dl_args = array(
        'labels'            => $dl_labels,
        'public'            => true,
        'publicly_queryable'=> true,
        'show_ui'           => true,
        'show_in_menu'      => true,
        'show_in_rest' =>true,
        'query_var'         => true,
        'capability_type'   => 'post',
        'has_archive'       => true,
        'hierarchical'      => false,
        'supports'          => apply_filters('dl_supports', array( 'title', 'editor', 'thumbnail', 'tags', 'comments', 'excerpt' ) ),
        'taxonomies' => array('downloads'),
    );
    register_post_type( 'dl', apply_filters( 'dl_post_type_args', $dl_args ) );

}

add_action('init', 'dl_setup_post_types');

function dl_categories() {
// set up labels
    $labels = array(
        'name'              => 'Download Categories',
        'singular_name'     => 'Download Category',
        'search_items'      => 'Search Download Categories',
        'all_items'         => 'All Download Categories',
        'edit_item'         => 'Edit Download Category',
        'update_item'       => 'Update Download Category',
        'add_new_item'      => 'Add New Download Category',
        'new_item_name'     => 'New Download Category',
        'menu_name'         => 'Download Categories'
    );
    // register taxonomy
    register_taxonomy( 'downloads', 'dl', array(
        'hierarchical' => true,
        'labels' => $labels,
        'query_var' => true,
        'show_ui' => true,
        'show_in_rest' =>true,
        'show_admin_column' => true,
        'rewrite' => array(
                'slug' => 'downloads',
        'hierarchical' => true,
            )
    ) );
}
add_action( 'init', 'dl_categories' );


/* Add tags */
add_action( 'init', 'dl_tags', 0 );
 
function dl_tags() {
 
// Labels part for the GUI
 
  $labels = array(
    'name' => _x( 'Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Tags' ),
    'popular_items' => __( 'Popular Tags' ),
    'all_items' => __( 'All Tags' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Tag' ), 
    'update_item' => __( 'Update Tag' ),
    'add_new_item' => __( 'Add New Tag' ),
    'new_item_name' => __( 'New Tag Name' ),
    'separate_items_with_commas' => __( 'Separate tags with commas' ),
    'add_or_remove_items' => __( 'Add or remove tags' ),
    'choose_from_most_used' => __( 'Choose from the most used tags' ),
    'menu_name' => __( 'Tags' ),
  ); 
 
// Now register the non-hierarchical taxonomy like tag
 
  register_taxonomy('tagged','dl',array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'tagged' ),
  ));
}

该插件在已有站点上正常运行多年,近期添加WooCommerce商城后出现问题:访问下载分类链接example.com/downloads/DownloadCategory1时显示404页面。禁用WooCommerce后链接恢复正常,咨询WooCommerce支持后得知问题出在我的插件上,尝试添加$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;、刷新固定链接等方法均无效,请问该如何解决?

解决方案

核心原因是你的自定义分类法downloads的slug和WooCommerce默认创建的**用户下载页面(slug同样为downloads)**产生了URL重写规则冲突,WordPress的重写规则匹配优先级导致WooCommerce的规则先被命中,从而返回404。

提供两种可行的解决办法:

方法一:修改自定义分类法的slug(推荐)

直接给自定义分类法设置一个不与WooCommerce冲突的唯一slug,比如dl-categories:
打开你的插件代码,找到dl_categories()函数中的register_taxonomy参数,修改rewrite数组里的slug值:

'rewrite' => array(
    'slug' => 'dl-categories', // 替换为不冲突的slug
    'hierarchical' => true,
)

方法二:调整分类法注册的优先级

如果不想修改slug,可以通过调整钩子优先级,让你的分类法在WooCommerce之后注册,从而让它的重写规则优先级更高:
将dl_categories()函数的钩子优先级从默认的10改为更高的数值(比如20),确保在WooCommerce完成页面注册后再加载你的分类法:

add_action( 'init', 'dl_categories', 20 );

最后必做步骤

完成上述任意一种修改后,必须进入WordPress后台的「设置 → 固定链接」页面,直接点击「保存更改」按钮(无需修改任何设置),强制刷新WordPress的重写规则缓存。

额外提示

  • WooCommerce默认会生成一个slug为downloads的页面,用于展示用户的已购下载记录,这是冲突的根源。
  • 如果选择修改slug,记得同步更新网站内所有指向旧分类链接的内容,避免出现死链。

内容的提问来源于stack exchange,提问作者user3061421

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 00:42:01