Php Wordpress自定义帖子类型正在重写所有其他帖子类型、页面和帖子的URL
我在我的主题中的functions.php文件中使用下面的代码设置了以下名为“Services”的CPTPhp Wordpress自定义帖子类型正在重写所有其他帖子类型、页面和帖子的URL,php,wordpress,mod-rewrite,wordpress-theming,custom-post-type,Php,Wordpress,Mod Rewrite,Wordpress Theming,Custom Post Type,我在我的主题中的functions.php文件中使用下面的代码设置了以下名为“Services”的CPT // Register Services Post Type function services_custom_post_type() { $labels = array( 'name' => _x( 'Services', 'Post Type General Name', 'text_domain' ),
// 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-Prevention而不是mydomain.com/Services/Fire-Prevention
我发现这句话:
'rewrite' => array('slug' => '/'),
在我的$args中,URL完全符合我的要求
问题是,现在我所有的其他页面和帖子都是404。
我发现如果我将永久链接更改为“普通”,那么它们仍然可以正常工作。但是我不能使用这个,它必须是永久链接的“Post name”
如果我从CPT中删除“rewrite”参数,那么所有其他页面都可以正常工作
我怎样才能使它成为
'rewrite' => array('slug' => '/'),
仅适用于预期的CPT,不影响我的其他页面和帖子
我的.htaccess文件很好,并且在Apache配置中将AllowOverride设置为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' );
这有帮助吗?的确如此!这就解决了问题。非常感谢