如果您想完全禁用前端上的自定义帖子类型单一视图,但能够显示存档页面,则事情会变得有些复杂。
设置publicly_queryable
于false
或rewrite
以false
防止用于显示单和归档视图。register_post_type
函数参数中没有标志,以防止仅创建单个视图重写规则。
https://github.com/WordPress/WordPress/blob/5.2.3/wp-includes/class-wp-post-type.php#L540
但是,您可以在注册帖子类型后删除重写标签,这将使存档视图重写规则保持不变,而仅删除单个视图重写规则。
/**
* Register event post type
*/
function wpse_128636_register_event_post_type() {
$labels = array(
'name' => __( 'Events' ),
'singular_name' => __( 'Event' ),
'add_new' => __( 'Add new' ),
'add_new_item' => __( 'Add new' ),
'edit_item' => __( 'Edit' ),
'new_item' => __( 'New' ),
'view_item' => __( 'View' ),
'search_items' => __( 'Search' ),
'not_found' => __( 'Not found' ),
'not_found_in_trash' => __( 'Not found Events in trash' ),
'parent_item_colon' => __( 'Parent' ),
'menu_name' => __( 'Events' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'title', 'page-attributes' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'event'),
'capability_type' => 'post',
);
register_post_type( 'event', $args );
remove_rewrite_tag( '%event%' ); // This line will remove event rewrite rules for single view
}
add_action( 'init', 'wpse_128636_register_event_post_type' );
另一个好处是,从现在开始,您可以使用事件发布类型的永久链接结构(event/simple-page
)创建简单的WordPress页面,这对复杂的网站很有用。
记住在代码修改后刷新重写规则。
get_current_user_id()
计算结果为0