更改自定义帖子类型的 slug 会导致单个帖子类型和页面出现 404


Changing the slug for custom post type resulted in 404 for single post type and pages

我的网站有自定义帖子类型:产品并绑定到自定义分类法:类别。

目前,我特定产品的URL是这样的:

example.com/product/laptop/acer/acerproductname

因为我想删除蛞蝓和 URL 应该看起来像这样

example.com/laptop/acer/acerproductname

因此,为了删除它,我在注册自定义帖子类型时更改了 slug。

$slug = product."/%product_category%/%product_brand%";

$slug = "/%product_category%/%product_brand%";

现在邮政注册功能看起来像这样

 public function aw_register_post_type(){        
        // Labels
	$labels = array(
		'name' => __('Products','framework'),
		'singular_name' => __('Product','framework'),
		'add_new' => __('Add new','framework'),
		'add_new_item' => __('Add new product','framework'),
		'edit_item' => __('Edit','framework'),
		'new_item' => __('New product','framework'),
		'view_item' => __('View product','framework'),
		'search_items' => __('Search product','framework'),
		'not_found' =>  __('No product found','framework'),
		'not_found_in_trash' => __('No product found in trash','framework'), 
		'parent_item_colon' => '',
		'menu_name' => __('Products','framework')
	);
	
	$short_url = (get_option('tz_products_short_url') != '') ? get_option('tz_products_short_url') : 0;
	$slug_first_part = ((get_option('tz_custom_products_slug') != '') ? get_option('tz_custom_products_slug') : 'product');
	if($short_url == 1) {
		$slug = $slug_first_part;
		
	} else {
		$slug = "/%product_category%/%product_brand%";
	}
	$rewrite = array( 
    	      	'slug'          => $slug,
		 //'with_front'    => false 
);
	
	// Arguments
	$args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true, 
		'show_in_menu' => true, 
		'query_var' => true,
		'menu_icon' => get_stylesheet_directory_uri().'/img/admin/admin-products.png',
		'rewrite' => true,
		'capability_type' => 'post',
		//'rewrite' => array("slug" => $slug), // Permalinks format
		'rewrite' => $rewrite,
		'has_archive' => true, 
		'hierarchical' => false,
		'menu_position' => null,
		'taxonomies' => array('post_tag'),
		'supports' => array('title','editor','author','thumbnail','excerpt', 'comments', 'tags')
	);

现在自定义帖子类型的 URL 正是我想要的

 example.com/laptop/acer/acerproductname

主要问题

更改 slug 后,我的博客和其他页面现在显示 404 错误。可能的原因是什么,可能的解决方案是什么。

尝试将其添加到您的函数中.php

flush_rewrite_rules();

刷新页面后将其注释掉。