简体   繁体   中英

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

My website have custom post type : products and tied to custom taxonomy : categories.

Currently my URLs for particular product is like this:

example.com/product/laptop/acer/acerproductname

As I wanted to remove the slug and URLs should look like this

example.com/laptop/acer/acerproductname

so to remove this, I changed the slug while registering custom post type.

from

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

to

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

Now the post register function look like this

  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') ); 

Now the URLs for custom post type is exactly what I wanted that is

 example.com/laptop/acer/acerproductname

Main Problem

After changing the slug my blogs and other pages now showing 404 error. What can be the possible reason and what can be the possible solution for this.

Try adding this into your functions.php

flush_rewrite_rules();

Comment it out once you've refreshed the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM