已解决:自定义分类法与 Woocommerce 冲突?

问题描述 投票:0回答:1

我编写了一个自定义插件,仅设置自定义帖子类型(dl)、其分类/类别(下载)和类似标签的分类(标记)。

它位于一个已经建立的网站上,并且该插件多年来一直运行良好。现在我想将 Woocommerce 商店添加到我的网站,但它导致了问题。

如果我想查看下载类别:

example.com/downloads/DownloadCategory1

它只显示404页面。

这是代码:

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 -> 链接再次开始工作。

我在 Woocommerce 支持论坛上询问过,但显然这不是 WC 问题,而是与我的插件有关。我不太确定可能出了什么问题。

我在这里和 StackExchange 上发现了类似的问题(自定义分类为 404),但似乎没有解决方案有效。我尝试添加

$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;

在注册分类法之后,根据此类论坛帖子的建议。没成功。

每次更改后我都会刷新永久链接,看看它是否有效。

已修复:我在 Woocommerce 设置 -> 高级中更改了下载的端点名称。

wordpress woocommerce custom-taxonomy
1个回答
0
投票

您应该为您的分类指定一个不同的名称。尝试更换:

register_taxonomy( 'downloads'

与:

register_taxonomy('downloads_dl'

然后刷新永久链接并检查其是否有效。

您可能不会有任何帖子,因此您必须单击每个帖子并检查新分类法,或者您可以只运行自定义 MySQL 查询。

© www.soinside.com 2019 - 2024. All rights reserved.