由于某些原因,我无法创建具有相同URL结构的多个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' ),
));
} );我想要的是什么?怎么做呢?
进一步发现
1
因为我正在进一步研究这个..。似乎wordpress会重定向以下内容(不需要我做任何额外的配置).
/cpt2/overview/
/cpt2/events/至
/cpt2/overview-2/
/cpt2/events-2/2
我找到了下面的wp函数弹状物 (带有可用的过滤器),如果它找到一个副本(附加-2,-3等),它会检查段塞是否返回唯一的段塞。如果您查看函数本身,它确实会执行post_type检查,但只有当post_type设置为非层次结构时。否则,它会找到所有层次的post_types,并检查来自所有(如文章、页面、书籍、事件)的唯一性。(例如)。
发布于 2013-06-21 20:46:26
这是我对上述问题的解决方案,我希望得到更多的反馈和/或更好的解决方案。下面的代码来自弹状物,它只检查自定义post类型和层次结构中的段塞唯一性。
注意:这是因为cpt1和cpt2使用他们自己的permalink前缀.
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 );https://stackoverflow.com/questions/17178100
复制相似问题