出于某种原因,我无法使用相同的网址结构创建多个CPT,例如我无法使用以下内容:
/cpt1/overview
/cpt1/events
/cpt2/overview
/cpt2/events
最终发生的事情如下:
/cpt1/overview
/cpt1/events
/cpt2/overview-2
/cpt2/events-2
我在干净安装wp时尝试了以下操作,以确保没有任何东西搞乱它:
add_action( 'init', function()
{
register_post_type( 'cpt1', array(
'labels' => array(
'name' => __('CPT1'),
'singular_name' => _x('Page', 'singular name'),
'add_new' => _x('Add New', 'page'),
'all_items' => __('All Pages'),
'add_new_item' => __('Add New Page'),
'edit_item' => __('Edit Page'),
'new_item' => __('New Page'),
'view_item' => __('View Page'),
'search_items' => _x('Search Pages', 'plural'),
'not_found' => _x('No pages found', 'plural'),
'not_found_in_trash' => _x('No pages found in Trash', 'plural'),
'parent_item_colon' => '',
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'cpt1', 'with_front' => false ),
'show_in_nav_menus' => false,
'hierarchical' => true,
'supports' => array( 'author', 'title', 'editor', 'custom-fields', 'page-attributes', 'revisions' ),
));
} );
add_action( 'init', function()
{
register_post_type( 'cpt2', array(
'labels' => array(
'name' => __('CPT2'),
'singular_name' => _x('Page', 'singular name'),
'add_new' => _x('Add New', 'page'),
'all_items' => __('All Pages'),
'add_new_item' => __('Add New Page'),
'edit_item' => __('Edit Page'),
'new_item' => __('New Page'),
'view_item' => __('View Page'),
'search_items' => _x('Search Pages', 'plural'),
'not_found' => _x('No pages found', 'plural'),
'not_found_in_trash' => _x('No pages found in Trash', 'plural'),
'parent_item_colon' => '',
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'cpt2', 'with_front' => false ),
'show_in_nav_menus' => false,
'hierarchical' => true,
'supports' => array( 'author', 'title', 'editor', 'custom-fields', 'page-attributes', 'revisions' ),
));
} );
我可能会追求什么?怎么样?
进一步发现
正如我正在进一步研究这个......似乎wordpress会重定向以下内容(不管我做任何额外的配置)......
/cpt2/overview/
/cpt2/events/
到
/cpt2/overview-2/
/cpt2/events-2/
我找到了以下wp函数wp_unique_post_slug(带有一个可用的过滤器),它检查slugs页面/帖子如果找到重复(附加-2,-3等)返回一个唯一的slug ..如果你看看函数本身,它确实进行了post_type检查,但只有当post_type设置为非分层时...否则它会找到所有分层post_types并检查所有的唯一性(例如post,page,book,event ..作为示例)。
答案 0 :(得分:1)
这是我对上述问题的解决方案,我希望得到更多反馈和/或更好的解决方案...以下使用来自wp_unique_post_slug的代码,该代码仅在自定义帖子类型和内部检查slug唯一性层次结构。
注意:这是有效的,因为cpt1和cpt2使用自己的固定链接前缀...
add_filter( 'wp_unique_post_slug', function( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug )
{
if ( in_array( $post_type, array( 'cpt1', 'cpt2' ) ) )
{
// start with a base slug w/o any suffixes
$slug = preg_replace( '/(-\d+)$/', '', $slug );
global $wpdb, $wp_rewrite;
$feeds = $wp_rewrite->feeds;
if ( ! is_array( $feeds ) )
$feeds = array();
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s 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 ) );
if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
$suffix = 2;
do {
$alt_post_name = substr( $slug, 0, 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 );