好的,我和你的问题类似,现在我相信我已经解决了.
我不确定我的代码会准确反映你需要做什么,但它应该很容易适应.请注意归档URL的重写规则.
的functions.php
function my_custom_posts() {
$labels = array(
'name' => _x( 'Events', 'post type general name' ),
'singular_name' => _x( 'Event', 'post type singular name' ),
'add_new' => _x( 'Add New', 'event' ),
'add_new_item' => __( 'Add New Event' ),
'edit_item' => __( 'Edit Event' ),
'new_item' => __( 'New Event' ),
'all_items' => __( 'All Events' ),
'view_item' => __( 'View Event' ),
'search_items' => __( 'Search Events' ),
'not_found' => __( 'No events found' ),
'not_found_in_trash' => __( 'No events found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Events'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our events and event specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title' ),
'has_archive' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => 'events', 'with_front' => true),
'capability_type' => 'post',
'hierarchical' => false,
);
register_post_type( 'event', $args );
//flush_rewrite_rules();
}
add_action( 'init', 'my_custom_posts' );
add_action('init', 'event_archive_rewrite');
function event_archive_rewrite(){
add_rewrite_rule('^events/([0-9]{4})/([0-9]{2})/?','index.php?post_type=event&year=$matches[1]&monthnum=$matches[2]','top');
add_rewrite_rule('^events/([0-9]{4})/?','index.php?post_type=event&year=$matches[1]','top');
}
这现在允许我访问这样的事件:
example.com/events/2013/07/01
请记住准备好适当的存档模板,例如
archive-event.php
此外,如果您只是注册帖子类型,则需要刷新永久链接缓存.通过进入WordPress管理员并再次“保存”永久链接结构来做到这一点.
真的希望这有助于某人!
谢谢,米奇.