【发布时间】:2020-08-25 16:52:38
【问题描述】:
我在我的主题中的 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 NOT mydomain.com/services/fire-prevention .
我发现这一行:
'rewrite' => array('slug' => '/'),
在我的 $args 中,该 URL 完全符合我的要求。
问题是,现在我所有的其他页面和帖子都是 404ing。
我发现如果我将永久链接更改为“普通”,那么它们仍然可以正常工作。不过我不能使用它,它必须是永久链接的“帖子名称”。
如果我从 CPT 中删除“重写”参数,那么所有其他页面都可以正常工作。
我怎样才能做到这一点
'rewrite' => array('slug' => '/'),
仅适用于预期的 CPT,不影响我的其他页面和帖子?
我的 .htaccess 文件很好,并且 AllowOverride 在 Apache conf 中设置为 ALL。
【问题讨论】:
-
确实如此!那解决了它。非常感谢
标签: php wordpress mod-rewrite wordpress-theming custom-post-type