Using Dates in Custom post_type Permalinks in Word

2019-04-08 02:55发布

I'm adding a custom post_type to Wordpress, and would like the permalink structure to look like this:

/%post_type%/%year%/%monthnum%/%postname%/

I can't figure out how to add the date tags. Using this code, gives me /my_type/example-post-slug/:

register_post_type( 'customtype', array(
    ...other options...
    'rewrite' => array('slug' => 'my_type'),
));

How do I include the dates?

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-08 03:26

You can achieve this with the plugin Custom Post Type Permalinks. Just install the plugin and change the permalink format in the settings.

查看更多
Emotional °昔
3楼-- · 2019-04-08 03:31

Use this it's working 100% :

'rewrite' => array('slug'=>date('Y').'/'.date('m').'/custom_post_type_slug','with_front'=>true)
查看更多
Juvenile、少年°
4楼-- · 2019-04-08 03:33

I have found a partial solution which allows for the permalink to be recognized and preserved upon loading the page in the address bar, but not updated in the edit screen or other links to the post on the site. Add the following to functions.php or a site specific plugin, replacing example-post-type with the identifier of your post type.

function example_rewrite() {
  add_rewrite_rule('^example-post-type/([0-9]{4})/([0-9]{1,2})/([^/]*)/?','index.php?post_type=example-post-type&year=$matches[1]&monthnum=$matches[2]&name=$matches[3]','top');
}
add_action('init', 'example_rewrite');

This uses the Rewrite API documented here To find more tips on understanding the process see here.

One thing to bear in mind is no matter how you do this, it is impossible for two posts to have the same slug, even if they have different dates. This is because if the permalink scheme is ever changed, they could clash and cause errors.

查看更多
登录 后发表回答