我知道有两种不同类型的自定义帖子类型:-页面帖子。
哪里page 支持子页,以及post 没有。
然而,当我试图在slug中创建一个只有数字的子页面时,wordpress将-2放在后面。示例:
/posttype/父/10-2/
而不是我想要的:
/posttype/父/10/
为什么会这样,我该如何解决?我已经搜索了好几个小时,但我似乎找不到解决方案,除了Wordpress中可能存在的一个限制,它与date permalink系统存在冲突。我没有使用这个系统,但这是真的吗?
编辑,更多信息:没有任何帖子会与我的永久链接冲突。permalink绝对不会被使用。
我通过全新安装wordpress获得了这种行为,并且只有1种自定义帖子类型。wordpress数据库中的帖子只有“parent”和“02”。其中“02”变为“02-2”。
我想知道是否分页/slug/page/02可能是数字slug不被接受的原因?
重要的是要注意,我只得到数字段塞,/父/子/不是问题。
我已经看到了一些关于覆盖过滤器的内容,但这难道不能简单地隐藏问题吗?我宁愿解决它。
我用于注册自定义帖子类型的代码:
$labels = array(
\'name\' => _x( $options[\'euthus_posttype_meervoud\'], \'Post Type General Name\', \'text_domain\' ),
\'singular_name\' => _x( $options[\'euthus_posttype\'], \'Post Type Singular Name\', \'text_domain\' ),
\'menu_name\' => __( $options[\'euthus_posttype_meervoud\'], \'text_domain\' ),
\'name_admin_bar\' => __( $options[\'euthus_posttype_meervoud\'], \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Item:\', \'text_domain\' ),
\'all_items\' => __( \'All Items\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Item\', \'text_domain\' ),
\'add_new\' => __( \'Add New\', \'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\' ),
\'search_items\' => __( \'Search Item\', \'text_domain\' ),
\'not_found\' => __( \'Not found\', \'text_domain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'text_domain\' ),
);
$rewrite = array(
\'slug\' => $options[\'euthus_posttype_baseurl\'],
\'with_front\' => true,
\'pages\' => false,
\'feeds\' => true,
);
$args = array(
\'label\' => __( \'euthus_childs\', \'text_domain\' ),
\'description\' => __( $options[\'euthus_posttype_meervoud\'], \'text_domain\' ),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'thumbnail\', \'page-attributes\',),
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 20,
\'menu_icon\' => \'dashicons-networking\',
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'rewrite\' => $rewrite,
\'capability_type\' => \'page\',
);
register_post_type( \'euthus_childs\', $args );
据我所知,“capability\\u type”必须设置为
page 而不是
post 要允许分层,将其设置为“post”不允许这样做。
最合适的回答,由SO网友:bonger 整理而成
正如您所猜测的和@Rarst所怀疑的,这里有一个分页签入wp_unique_post_slug()
对于分级职位类型:
preg_match( "@^($wp_rewrite->pagination_base)?\\d+$@", $slug )
它将匹配任何纯数字的slug,可以选择前面加“page”。要绕过这个问题,您可以使用
\'wp_unique_post_slug\'
过滤器,基本上复制原始代码而不进行分页检查,例如
add_filter( \'wp_unique_post_slug\', function ( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
if ( $slug !== $original_slug && is_post_type_hierarchical( $post_type ) ) {
global $wpdb, $wp_rewrite;
$slug = $original_slug; // Undo any previous processing.
// The following is just a copy & paste of the WP code without the pagination check.
$feeds = $wp_rewrite->feeds;
if ( ! is_array( $feeds ) )
$feeds = array();
if ( \'nav_menu_item\' == $post_type )
return $slug;
/*
* Page slugs must be unique within their own trees. Pages are in a separate
* namespace than posts so page slugs are allowed to overlap post slugs.
*/
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, \'attachment\' ) AND ID != %d AND post_parent = %d LIMIT 1";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
/**
* Filter whether the post slug would make a bad hierarchical post slug.
*
* @since 3.1.0
*
* @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context.
* @param string $slug The post slug.
* @param string $post_type Post type.
* @param int $post_parent Post parent ID.
*/
if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( \'wp_unique_post_slug_is_bad_hierarchical_slug\', false, $slug, $post_type, $post_parent ) ) {
$suffix = 2;
do {
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) );
$suffix++;
} while ( $post_name_check );
$slug = $alt_post_name;
}
}
return $slug;
}, 10, 6 );