Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this questionIt seems terribly unintuitive to me...
I've looked through dozens of examples on different resources but still can't understand how to change the custom post type slug with this, or completely remove the slug of a certain category.
The site have custom post type "news-items", and the final URL should have just "news", also without redirection to a static page.
sitename/news-items/
sitename/news-items/some-random-article
to
sitename/news/
sitename/news/random-article
The site have the category "catalog", inside which can be (or can not) random number of subcategories. And this "catalog" slug should be completely removed from the URL.
sitename/catalog/random-subcategory/
sitename/catalog/random-subcategory/random-item
sitename/catalog/random-subcategory/random-sub-subcategory/random-item
to
sitename/random-subcategory/
sitename/random-subcategory/random-item
sitename/random-subcategory/random-sub-subcategory/random-item
Is it even possible to do such things without using plugins or overcomplicated hack-like looking solutions?
Also, should flush_rewrite_rules() really be used? Because some of the examples I looked at used it, but many others did not.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this questionIt seems terribly unintuitive to me...
I've looked through dozens of examples on different resources but still can't understand how to change the custom post type slug with this, or completely remove the slug of a certain category.
The site have custom post type "news-items", and the final URL should have just "news", also without redirection to a static page.
sitename.com/news-items/
sitename.com/news-items/some-random-article
to
sitename.com/news/
sitename.com/news/random-article
The site have the category "catalog", inside which can be (or can not) random number of subcategories. And this "catalog" slug should be completely removed from the URL.
sitename.com/catalog/random-subcategory/
sitename.com/catalog/random-subcategory/random-item
sitename.com/catalog/random-subcategory/random-sub-subcategory/random-item
to
sitename.com/random-subcategory/
sitename.com/random-subcategory/random-item
sitename.com/random-subcategory/random-sub-subcategory/random-item
Is it even possible to do such things without using plugins or overcomplicated hack-like looking solutions?
Also, should flush_rewrite_rules() really be used? Because some of the examples I looked at used it, but many others did not.
Part 1 Ans: I am presuming you have added the custom post type yourself, in which case it is easy.
When you register a new post type, you can set any rewrite rules for that post type to follow as part of the arguments used in the register_post_type( 'name', $args ) function.
If your post type is available on the front end and is public, then by default WordPress will use the custom post name as the slug. You can override this as follows:
$args = array(
// your other arguments
'rewrite' => array(
'slug' => 'products', // use this slug instead of post type name
'with_front' => FALSE // if you have a permalink base such as /blog/ then setting this to false ensures your custom post type permalink structure will be /products/ instead of /blog/products/
),
);
register_post_type( 'portfolio', $args );
The other arguments you can use are documented at http://codex.wordpress.org/Function_Reference/register_post_type
Part 2 Ans: For achieving below
sitename.com/catalog/random-subcategory/ sitename.com/catalog/random-subcategory/random-item sitename.com/catalog/random-subcategory/random-sub-subcategory/random-item to sitename.com/random-subcategory/ sitename.com/random-subcategory/random-item sitename.com/random-subcategory/random-sub-subcategory/random-item
If you want to remove /catalog/ from the url, follow these two steps:
Go to Settings >> Permalinks and select Custom and enter: /%category%/%postname%/ Next set your Category Base to . Save it and you’ll see your URL changed to this format: sitename.com/random-subcategory/
.htaccess
directives) or via WordPress's rewrite rules APIs (in which case yes, you should either callflush_rewrite_rules()
one single time - not every page load - or just go hit submit on the permalink settings page). But it's not clear why you would use a rewrite rule for the CPT, instead of just changing the CPT's slug as Priyanka suggests. As Pat mentions, more information regarding your efforts thus far would also be useful. – bosco Commented Dec 30, 2024 at 19:01