Using Dates in Custom post_type Permalinks in Wordpress 3.0

血红的双手。 提交于 2019-12-06 01:28:47

问题


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?


回答1:


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




回答2:


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.




回答3:


Use this it's working 100% :

'rewrite' => array('slug'=>date('Y').'/'.date('m').'/custom_post_type_slug','with_front'=>true)


来源:https://stackoverflow.com/questions/3136518/using-dates-in-custom-post-type-permalinks-in-wordpress-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!