'Custom post with custom URL
I have created a custom post, and I would like the URL to be:
example.com/directory/profile/slug/
With that in mind, this is a snap shot of how I have set it up:
$args = array(
...
'rewrite' => array( 'slug' => 'directory/profile' ),
'supports' => array( 'title', 'thumbnail' ),
);
I have created a corresponding file:
single-profile.php
If I remove the directory/
from the rewrite parameter, the new post shows up at this URL:
example.com/profile/slug/
I have looked at adding rewrite rules but either thats not the right approach or I am not doing that correctly.
Example:
add_rewrite_rule(
'^directory/profile/([^/]+)/?$',
'index.php?pagename=$matches[1]&post_type=profile',
'top'
);
What extra steps do I need to get this to work as intended?
Solution 1:[1]
An easy way to do it is: go to settings > permalinks
, select Custom and set the value to /%postname%/
.
No need for the rewrite rules.
Keep the post type slug as directory/profile
.
Note that this will also change the default posts URLs to /postname
. If you want to apply this change only for the post type, you can use this plugin, making the change above only for this post type.
Or do it on the code using the post_type_link hook, also works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | André Pereira |