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

szqfcxe2  于 5个月前  发布在  WordPress
关注(0)|答案(1)|浏览(53)

我写了一个自定义插件,它只设置自定义文章类型(dl),分类/类别(下载)和类似标签的分类(taged)。
它在一个已经建立的网站上,该插件已经工作了多年。现在我想在我的网站上添加一个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;


在register_taxonomy之后,从一个这样的论坛帖子的建议。没有工作。
每次更改后,我都会刷新永久链接,看看它是否有效。

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

noj0wjuj

noj0wjuj1#

您应该给予您的分类法一个不同的名称。尝试替换:

register_taxonomy( 'downloads'

字符串
有:

register_taxonomy('downloads_dl'


然后刷新永久链接并检查它是否工作。
你可能不会有任何帖子,所以你必须点击每一篇帖子并给予新的分类,或者你可以运行一个自定义的MySQL查询。

相关问题