2

出于某种原因,我无法创建具有相同 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 函数wp_unique_post_slug(带有可用的过滤器),它检查页面/帖子的 slug,如果它找到重复项(附加 -2、-3 等),则返回一个唯一的 slug .. 如果你查看函数本身,它确实会进行 post_type 检查,但前提是 post_type 设置为非分层 .. 否则它会查找所有分层 post_types 并检查所有的唯一性(例如,帖子、页面、书籍、事件 .. 作为示例)。

4

1 回答 1

2

这是我对上述问题的解决方案,我希望得到更多反馈和/或更好的解决方案......下面使用来自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 );
于 2013-06-21T20:46:26.950 回答