【问题标题】:Changing the slug for custom post type resulted in 404 for single post type and pages更改自定义帖子类型的 slug 导致单个帖子类型和页面的 404
【发布时间】:2016-11-14 09:37:22
【问题描述】:

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

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

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%";

现在post register函数是这样的

 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 错误。可能的原因是什么,可能的解决方案是什么。

【问题讨论】:

  • 如果取消注释 'with_front' => false 是否有效?修改后你会做flush_rewrite_rules()吗?
  • 取消注释没有任何区别。不,我没有。
  • 刷了还是没区别

标签: php wordpress custom-post-type slug


【解决方案1】:

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

flush_rewrite_rules();

刷新页面后将其注释掉。

【讨论】:

  • 已经试过了。没用。正如与其他一些开发人员讨论的那样。他说需要在functions.php中添加另一个重写规则行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-01
  • 2017-08-26
  • 2017-09-19
  • 2012-01-01
  • 2016-06-05
  • 1970-01-01
  • 2017-03-30
相关资源
最近更新 更多