我已经使用主题中functions.php文件中的以下代码设置了以下名为"Services"的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', 'text_domain' ),
'menu_name' => __( 'Services', 'text_domain' ),
'name_admin_bar' => __( 'Service', 'text_domain' ),
'archives' => __( 'Service Archives', 'text_domain' ),
'attributes' => __( 'Service Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Services', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'add_new' => __( 'Add Service', 'text_domain' ),
'new_item' => __( 'New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'view_items' => __( 'View Items', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Service', 'text_domain' ),
'description' => __( 'My Services', 'text_domain' ),
'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-proventionNOTmydomain.com/Services/Fire-provention。
我发现这条线:
'rewrite' => array('slug' => '/'),
在我的$args中,URL正按照我的意愿运行。
问题是,现在我所有的其他页面和帖子都是404
我发现如果我把永久链接改为"普通",那么它们仍然可以正常工作。不过我不能用这个,它必须是永久链接的"Post-name"。
如果我从CPT中删除"重写"参数,那么所有其他页面都可以正常工作。
我如何才能使
'rewrite' => array('slug' => '/'),
只为预期的CPT工作,不影响我的其他页面和帖子?
我的.htaccess文件很好,并且AllowOverride在Apache conf.中设置为ALL
所以这个对另一个问题的回答为我解决了这个问题。
我在我的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' );