2013-06-18 75 views
2

出於某種原因,我不能創建具有相同的URL結構多CPT,例如我不能讓下面的工作:多個自定義文章類型,相同的URL結構

/cpt1/overview 
/cpt1/events 

/cpt2/overview 
/cpt2/events 

結束意外事件發生的是以下:

/cpt1/overview 
/cpt1/events 

/cpt2/overview-2 
/cpt2/events-2 

我試圖在一個乾淨的安裝之後可溼性粉劑,以確保沒有被搞亂起來:

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(帶有一個可用的過濾器),它檢查頁/帖子的段落,如果它發現重複(附加-2,-3等),則返回一個獨特的段落。在函數本身,它確實做了post_type檢查,但是隻有當post_type被設置爲非等級時,否則它會找到所有的分層post_types並檢查所有的唯一性(例如帖子,頁面,書籍,活動..作爲例子)。

回答

1

這是我對上述問題的解決方案,我希望獲得更多反饋意見和/或更好的解決方案......下面的代碼使用wp_unique_post_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)?\[email protected]", $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);