问题描述
我已在主题内我的functions.PHP文件中使用以下代码设置了名为“服务”的以下CPT。
// Register Services Post Type
function services_custom_post_type() {
$labels = array(
'name' => _x( 'Services','Post Type General Name','text_domain' ),'singular_name' => _x( 'Service','Post Type Singular Name','menu_name' => __( 'Services','name_admin_bar' => __( 'Service','archives' => __( 'Service Archives','attributes' => __( 'Service Attributes','parent_item_colon' => __( 'Parent Item:','all_items' => __( 'All Services','add_new_item' => __( 'Add New Item','add_new' => __( 'Add Service','new_item' => __( 'New Item','edit_item' => __( 'Edit Item','update_item' => __( 'Update Item','view_item' => __( 'View Item','view_items' => __( 'View Items','search_items' => __( 'Search Item','not_found' => __( 'Not found','not_found_in_trash' => __( 'Not found in Trash','featured_image' => __( 'Featured Image','set_featured_image' => __( 'Set featured image','remove_featured_image' => __( 'Remove featured image','use_featured_image' => __( 'Use as featured image','insert_into_item' => __( 'Insert into item','uploaded_to_this_item' => __( 'Uploaded to this item','items_list' => __( 'Items list','items_list_navigation' => __( 'Items list navigation','filter_items_list' => __( 'Filter items list',);
$args = array(
'label' => __( 'Service','description' => __( 'My Services','labels' => $labels,'supports' => array( 'title','editor','page-attributes' ),'hierarchical' => true,'public' => true,'show_ui' => true,'show_in_menu' => true,'menu_position' => 5,'show_in_admin_bar' => true,'show_in_nav_menus' => true,'can_export' => true,'has_archive' => true,'exclude_from_search' => false,'publicly_queryable' => true,'capability_type' => 'page','menu_icon' => 'dashicons-palmtree','rewrite' => array('slug' => '/'),);
register_post_type( 'services',$args );
}
add_action( 'init','services_custom_post_type',0 );
我希望这些CPT的URL中不要包含“服务”一词。因此,对于名为“防火”的服务帖子,我希望URL为 mydomain.com/fire-prevention 否 mydomain.com/services/fire-prevention 。
我在一行中找到了
'rewrite' => array('slug' => '/'),
在我的$ args中,URL的运行完全符合我的要求。
问题是,现在我所有其他页面和帖子都在404处理。
我发现如果我将永久链接更改为“普通”,那么它们仍然可以正常工作。但是,我不能使用它,它必须是永久链接的“职位名称”。
如果我从CPT中删除了“ rewrite”参数,则所有其他页面都可以正常工作。
如何做到这一点
'rewrite' => array('slug' => '/'),
仅适用于预期的CPT,不会影响我的其他页面和帖子吗?
我的.htaccess文件很好,并且在Apache conf中将AllowOverride设置为ALL。
解决方法
所以this answer on another question resolved it for me.
我在我的functions.php中添加了以下内容,并且可以正常工作:
function na_remove_slug( $post_link,$post,$leavename ) {
if ( 'services' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/','/',$post_link );
return $post_link;
}
add_filter( 'post_type_link','na_remove_slug',10,3 );
function na_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type',array( 'post','services','page' ) );
}
}
add_action( 'pre_get_posts','na_parse_request' );