拼写错误时WordPress自定义分类URL重写

时间:2021-08-20 作者:macstens

我发现自己处于一种我想理解并最终解决的状况。我们有自定义的帖子类型,对于它们,还有自定义的分类法。

一切正常。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 );}
我希望这些信息足以说明问题所在。如果需要的话,我很乐意展示更多相关的代码。

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

除了最后一个URL部分外,更改哪个URL部分也无关紧要。因此,这仍然显示原始页面的内容。

是的,因为你的分类法rewrite 具有permalink结构的层次结构/treatment/<term slug> (例如。/treatment/psychosomatic) 或/treatment/<term slug>/<term slug>/... (如果术语为子术语,例如。/treatment/psychosomatic/psychosomatic-dysfunction), 因此,只要当前URL与该结构匹配<term slug> 是有效的术语slug,则页面将正常加载。

如何改变行为,使拼写错误的URL会导致404错误?

没有可以做到这一点的分类参数或管理设置,但您可以使用pre_handle_404 filter 这样:(您可以将其添加到主题函数文件)

add_filter( \'pre_handle_404\', \'my_pre_handle_404\', 10, 2 );
function my_pre_handle_404( $preempt, $wp_query ) {
    // A list of taxonomy slugs.
    $taxonomies = array( \'treatment\' );

    // Check if we\'re on the archive page for taxonomies in the above list.
    if ( $wp_query->is_tax( $taxonomies ) ) {
        // We need this to access the path of the current URL ($wp->request),
        // and note that the path does not include the trailing slashes.
        global $wp;

        // Get the original/correct term permalink.
        // e.g. https://example.com/treatment/parent/child/term-slug/
        $permalink = get_term_link( $wp_query->get_queried_object() );

        // Get the path in the above permalink.
        // e.g. treatment/parent/child/term-slug - traling slashes are trimmed
        $orig_path = trim( parse_url( $permalink, PHP_URL_PATH ), \'/\' );

        // Check if the above path matches the path of the current URL, and if
        // not, then we issue a 404 error.
        if ( $orig_path !== $wp->request ) {
            $wp_query->set_404();
        }
    }

    return $preempt;
}
如果希望代码使用默认值category 分类,然后更改$wp_query->is_tax( $taxonomies )$wp_query->is_tax( $taxonomies ) || $wp_query->is_category().

相关推荐

从Yoast SEO中检索一个值,以用于设置默认的Twitter卡片图像以实现覆盖

所以Yoast SEO有一个问题,您无法定义全局默认的twitter卡图像。因此,我希望两者都设置一个默认的twitter卡图像,但允许在特定页面上的覆盖实际工作。我当前正在使用:$default_opengraph = \'https://website.com/image.png\'; function default_opengraph() { global $default_opengraph; return $default_opengraph; } ad