php - Wordpress 自定义帖子类型正在重写所有其他帖子类型、页面和帖子的 URL
问题描述
我在我的主题中的 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', '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。
我发现如果我将永久链接更改为“普通”,那么它们仍然可以正常工作。不过我不能使用它,它必须是永久链接的“帖子名称”。
如果我从 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' );
推荐阅读
- c++ - OpenGL C++抽象和顶点绘制问题
- android - 在 Kotlin 中不推荐使用 ActiveNetworkInfo 和 isConnected
- python - 优步路德维希:问题预测
- visual-studio-code - vscode 工作空间可以使用相对路径
- angular - 路线不断被重定向
- python - 机器学习 - 如何从管道中提取特征
- python - 在 pandas DataFrame 中选择具有特定值的列
- r - 在 R 的一个文件夹中的多个 xml 文件上运行循环
- javascript - 如何使用将多个值传递给服务器使用 node.js 标记和访问
- django - Django taggit:为什么 annotate(same_tags=Count('tags')) 计算的是常用标签的数量而不是标签的总数?