如果您想在前端完全禁用自定义post类型的单一视图,但能够显示归档页面,事情就变得有点复杂了。
背景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 post类型的永久链接结构创建简单的WordPress页面(
event/simple-page
) 这对复杂的网站很有帮助。
记住在修改代码后刷新重写规则。