我发现自己处于一种我想理解并最终解决的状况。我们有自定义的帖子类型,对于它们,还有自定义的分类法。
一切正常。URL生成的权限。但是:无论出于何种原因,当有人更改URL的一部分或拼写错误时,就会发生一些奇怪的事情(在我看来)。首先调用此正确的URL:
/treatment/psychosomatic/psychosomatic-dysfunction/temporary-psychomatic-dysfunction/
/<custom post type>/<taxonomy level 0>/<taxonomy level 1>/<taxonomy level 2>/
通过在URL部分中输入完全不同的内容来更改URL,将显示原始URL的内容,并编制索引并返回HTTP状态代码200,这可能是未来的SEO问题。除了最后一个URL部分外,更改哪个URL部分也无关紧要。因此,这仍然显示原始页面的内容。
/treatment/exampletext/lorem-ipsum-dolor/temporary-psychomatic-dysfunction/
问题是,如何改变行为,使拼写错误的URL会导致404错误?
以下是帖子类型和分类注册码:
function cptui_register_my_cpts_clinic() {
$labels = [
"name" => __( "clinics", "spotter" ),
"singular_name" => __( "clinic", "spotter" ),
];
$args = [
"label" => __( "clinics", "spotter" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => [ "slug" => "clinic", "with_front" => false ],
"query_var" => true,
"menu_icon" => "dashicons-admin-home",
"supports" => [ "title", "editor", "thumbnail", "comments" ],
"show_in_graphql" => false,
];
register_post_type( "clinic", $args );}
以及
function cptui_register_my_taxes_treatment() {
$labels = [
"name" => __( "treatments", "spotter" ),
"singular_name" => __( "treatment", "spotter" ),
];
$args = [
"label" => __( "Treatment", "spotter" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => [ \'slug\' => \'treatment\', \'with_front\' => false, \'hierarchical\' => true, ],
"show_admin_column" => false,
"show_in_rest" => true,
"rest_base" => "treatment",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => true,
"show_in_graphql" => false,
];
register_taxonomy( "treatment", [ "clinic" ], $args );}
我希望这些信息足以说明问题所在。如果需要的话,我很乐意展示更多相关的代码。