php - Wordpress Custom_post slug Рё hurl
Get the solution ↓↓↓There is an issue that I cannot solve for a couple of days. All searches come down to the same thing ...
The question is as follows:
There is a needto create a new post type
for the convenience of filling the site structure, this is fraught with the fact that acustom_post_slug
is added to the url, which I want to get rid of.
It is necessary to save the structure of the hurl on the site of the following type:
site.com/category/post_name
site.com/category/category_child/post_name
due to the new post type, whatever one may say, it turns out only like this:
site.com/custom_post_slug/post_name
What I already did:
Most of the recommendations do not solve the problem, for example,replace slug with '/'
- it will simply remove slug from url.
There is a recommendation when creating a new post typeregister_post_type ()
to specify forrewrite => slug
with the value%category%
, this resolving my issue but after that all posts of the new post typeget 404
, and I don't understand what to do next.
Add some code:
function cptui_register_my_cpts_addresses() {
/**
* Post Type: Addresses.
*/
$labels = [
"name" => __( __("Addresses",'cnm_theme'), "custom-post-type-ui" ),
"singular_name" => __( __("Addresses",'cnm_theme'), "custom-post-type-ui" ),
];
$args = [
"label" => __( __("Addresses",'cnm_theme'), "custom-post-type-ui" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => [ "slug" => '%category%','with_front' => false ],
// "rewrite" => false,
"query_var" => true,
"supports" => [ "title", "editor", "thumbnail", "excerpt", "trackbacks", "custom-fields", "comments", "revisions", "author", "page-attributes", "post-formats" ],
"taxonomies" => [ "category", "post_tag" ],
];
register_post_type( "addresses", $args );
}
add_action( 'init', 'cptui_register_my_cpts_addresses' );
Answer
Solution:
I decide to leave that custom_slug because any changing of url is unwelcome to WP logic, so i leave custom_slug but change it to one letter like:
site.com**/a/category/category_child/**post_name
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: failed to create image decoder with message 'unimplemented'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.