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

时间:2016-07-12 08:41:41

标签: php wordpress custom-post-type slug

我的网站有自定义帖子类型:产品并与自定义分类法相关联:类别。

目前我的特定产品的网址是这样的:

example.com/product/laptop/acer/acerproductname

因为我想删除slug,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')
	);




现在,自定义帖子类型的网址正是我想要的

 example.com/laptop/acer/acerproductname

主要问题

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

1 个答案:

答案 0 :(得分:0)

尝试将此添加到您的functions.php

flush_rewrite_rules();

在您刷新页面后对其进行评论。