如何在使用自定义分类术语段塞添加自定义帖子类型URL后修复开机自检404

时间:2019-04-22 作者:Dzodzos

我正在为帖子等设置“特殊”url结构

我想得到的结果是:sitename。com/club/justcavaliclub是分类法中的术语(club\\u类型),justcavali是post类型(booking\\u类型)

在我将“重写”设置为“俱乐部类型”后,我所有的帖子/页面都会转到404,例如,我在这些页面上得到一些毫无意义的查询post\\u type=“附件”,

我已经尝试了每一种方法来解决这个问题,但有些人要么分类法/cpt中断,要么帖子/页面中断

当我插入“%club\\u type%”为cpt重写时,它会中断,是的,我禁用了“with front”,并且我正在从术语中删除分类库

club\\u类型/俱乐部->/俱乐部

function cptui_register_my_cpts_listing() {

/**
 * Post Type: Listing.
 */

$labels = array(
    "name" => __( "Listing", "sage" ),
);

$args = array(
    "label" => __( "Listing", "sage" ),
    "labels" => $labels,
    "description" => "New Listinga",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "delete_with_user" => false,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => true,
    "rewrite" => array( "slug" => "/%club_type%/", "with_front" => false ),
    "query_var" => true,
    "supports" => array( "title", "editor" ),
    "taxonomies" => array( "club_type", "city_part" ),
);

register_post_type( "listing", $args );
    }

    add_action( \'init\', \'cptui_register_my_cpts_listing\' );

function cptui_register_my_taxes_club_type() {

/**
 * Taxonomy: Club Type
 */

$labels = array(
    "name" => __( "club_type", "sage" ),
    "singular_name" => __( "club_type", "sage" ),
);

$args = array(
    "label" => __( "club_type", "bgn" ),
    "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" => array( \'slug\' => \'club_type\', \'with_front\' => false, ),
    "show_admin_column" => true,
    "show_in_rest" => true,
    "rest_base" => "club_type",
    "rest_controller_class" => "WP_REST_Terms_Controller",
    "show_in_quick_edit" => true,
    );
register_taxonomy( "club_type", array( "listing" ), $args );
}
add_action( \'init\', \'cptui_register_my_taxes_club_type\' );

1 个回复
SO网友:sMyles

首先,您不需要在init, 它们都可以在相同的操作函数下完成——因此将寄存器CPT和分类法移动到单个函数(或从主函数调用单独的函数)——无需有两个init操作。

您还应该在CPT之前注册分类法(如果您不想更改代码,只需以低于CPT注册的优先级调用分类法操作),否则请将分类法注册码置于CPT注册码之上。

"rewrite" => array( "slug" => "/%club_type%/", "with_front" => false )
也可以是没有斜杠的:

"rewrite" => array( "slug" => "%club_type%", "with_front" => false ),
虽然我建议您在slug中添加某种标识符,比如listing/%club_type% 否则WordPress将尝试将其用作特定页面的查询。

同时检查以下各项:

Permalinks: custom post type -> custom taxonomy -> post

Mixing custom post type and taxonomy rewrite structures?

您还可以查看可以实现此功能的不同插件:https://wordpress.org/plugins/custom-post-type-permalinks/

我怀疑您的问题可能是因为您在分类法之前没有使用任何类型的标识符,因此它试图将其作为帖子名称来查找,而不是作为分类法的slug

相关推荐