2014-11-06 63 views
0

下面的成功讓我使用程序計劃常見問題解答在我的WordPress網站,定製文章類型但我與網頁的永久鏈接掙扎。WordPress的自定義文章類型不能訪問子頁

當我創建一個新的FAQ頁面我設置父爲程序,這是好的,但它是我似乎無法獲得工作的永久鏈接。

如果示例程序被稱爲足球我將訪問domain.com/programmes/football然後我會創建具有相同名稱足球程序常見問題,並從程序設置父爲足球和常見問題的永久將成爲domain.com/programme_faq/football/football

當我嘗試訪問該頁面時,它以404找不到。如果我從常見問題解答頁面中刪除父項選項,則永久鏈接將變爲domain.com/programme_faq/football,這可以工作。

理想我寧願在計劃常見問題解答作出最終頁爲domain.com/programmes/football/faq

add_action('init', 'register_cpt_programmes'); 
add_action('init', 'register_cpt_programmes_faq'); 

function register_cpt_programmes() { 

    $labels = array( 
     'name'    => __('Programmes', 'text_domain'), 
     'singular_name'  => __('single programme name', 'text_domain'), 
     'add_new'   => _x('Add Programme', '${4:Name}', 'text_domain'), 
     'add_new_item'  => __('Add Programme', 'text_domain}'), 
     'edit_item'   => __('Edit this Programme', 'text_domain'), 
     'new_item'   => __('New Programme', 'text_domain'), 
     'view_item'   => __('View Programme', 'text_domain'), 
     'search_items'  => __('Search Programmes', 'text_domain'), 
     'not_found'   => __('No Programmes found', 'text_domain'), 
     'not_found_in_trash' => __('No Programmes found in Trash', 'text_domain'), 
     'parent_item_colon' => __('Parent single post type name:', 'text_domain'), 
     'menu_name'   => __('Programmes', 'text_domain'), 
    ); 

    $args = array( 
     'labels'    => $labels, 
     'hierarchical'  => true, 
     'description'   => 'description', 
     //'taxonomies'   => array('category'), 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_menu'  => true, 
     'menu_position'  => 5, 
     //'menu_icon'   => '', 
     'show_in_nav_menus' => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'has_archive'   => true, 
     'query_var'   => true, 
     'can_export'   => true, 
     'rewrite'    => true, 
     'capability_type'  => 'page', 
     'supports'   => array( 
      'title', 'editor', 'author', 'page-attributes', 'thumbnail', 'excerpt', 'custom-fields', 'revisions', 'comments' 
     ), 
     'rewrite' => array(
      'with_front' => false, 
      'slug'  => 'programmes' 
     ) 
    ); 

    register_post_type('programmes', $args); 
} 

function register_cpt_programmes_faq() { 

    $labels = array( 
     'name'    => __('Programme FAQ', 'text_domain'), 
     'singular_name'  => __('single programme faq', 'text_domain'), 
     'add_new'   => _x('Add Programme FAQ', '${4:Name}', 'text_domain'), 
     'add_new_item'  => __('Add Programme FAQ', 'text_domain}'), 
     'edit_item'   => __('Edit this Programme FAQ', 'text_domain'), 
     'new_item'   => __('New Programme FAQ', 'text_domain'), 
     'view_item'   => __('View Programme FAQ', 'text_domain'), 
     'search_items'  => __('Search Programme FAQ', 'text_domain'), 
     'not_found'   => __('No Programme FAQs found', 'text_domain'), 
     'not_found_in_trash' => __('No Programmes FAQs found in Trash', 'text_domain'), 
     'parent_item_colon' => __('Parent single post type name:', 'text_domain'), 
     'menu_name'   => __('Programme FAQ', 'text_domain'), 
    ); 

    $args = array( 
     'labels'    => $labels, 
     'hierarchical'  => true, 
     'description'   => 'description', 
     //'taxonomies'   => array('category'), 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_menu'  => true, 
     'menu_position'  => 5, 
     //'menu_icon'   => '', 
     'show_in_nav_menus' => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'has_archive'   => true, 
     'query_var'   => true, 
     'can_export'   => true, 
     'rewrite'    => true, 
     'capability_type'  => 'page', 
     'supports'   => array( 
      'title', 'editor', 'author', 'page-attributes', 'thumbnail', 'excerpt', 'custom-fields', 'revisions', 'comments' 
     ), 
     'rewrite' => array(
      'with_front' => false, 
      'slug'  => 'programme_faq' 
     ) 
    ); 

    register_post_type('programme_faq', $args); 
} 
add_action('admin_menu', function() { 
    remove_meta_box('pageparentdiv', 'programme_faq', 'normal'); 
}); 

add_action('add_meta_boxes', function() { 
    add_meta_box('programmes-parent', 'Programmes', 'programmes_attributes_meta_box', 'programme_faq', 'side', 'high'); 
}); 

function programmes_attributes_meta_box($post) { 
    $post_type_object = get_post_type_object($post->post_type); 
    if ($post_type_object->hierarchical) { 
     $pages = wp_dropdown_pages(array('post_type' => 'programmes', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0)); 
     if (! empty($pages)) { 
      echo $pages; 
     } // end empty pages check 
    } // end hierarchical check. 
} 

回答

2

這是我第一次回答堆棧溢出。請原諒格式和拼寫問題。

內容:

在到達一個解決方案的關鍵步驟是記錄在代碼。這是一個總結:

  1. 註冊自定義文章類型
  2. 添加一個「父計劃」的編輯屏幕
  3. 在元框添加重寫規則,以便WordPress的正確分析自定義URL結構
  4. 過濾器在永久所以鏈接FAQ頁面可以正確地呈現

CODE:

// 1a. register the Programmes post type 
function register_cpt_programmes() 
{ 
    $labels = array(
     'name'    => __('Programmes', 'text_domain'), 
     'singular_name'  => __('single programme name', 'text_domain'), 
     'add_new'   => _x('Add Programme', '${4:Name}', 'text_domain'), 
     'add_new_item'  => __('Add Programme', 'text_domain}'), 
     'edit_item'   => __('Edit this Programme', 'text_domain'), 
     'new_item'   => __('New Programme', 'text_domain'), 
     'view_item'   => __('View Programme', 'text_domain'), 
     'search_items'  => __('Search Programmes', 'text_domain'), 
     'not_found'   => __('No Programmes found', 'text_domain'), 
     'not_found_in_trash' => __('No Programmes found in Trash', 'text_domain'), 
     'parent_item_colon' => __('Parent single post type name:', 'text_domain'), 
     'menu_name'   => __('Programmes', 'text_domain'), 
    ); 

    $args = array(
     'labels'    => $labels, 
     'hierarchical'  => false, 
     'description'   => 'description', 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_menu'  => true, 
     'menu_position'  => 5, 
     'show_in_nav_menus' => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'has_archive'   => true, 
     'query_var'   => true, 
     'can_export'   => true, 
     'rewrite'    => true, 
     'capability_type'  => 'page', 
     'supports'   => array(
      'title', 
      'editor', 
      'author', 
      'page-attributes', 
      'thumbnail', 
      'excerpt', 
      'custom-fields', 
      'revisions', 
      'comments' 
     ), 
     'rewrite' => array(
      'with_front' => false, 
      'slug'  => 'programmes' 
     ) 
    ); 
    register_post_type('programmes', $args); 
} 
add_action('init', 'register_cpt_programmes'); 

// 1b. register the Programme FAQ post type 
function register_cpt_programme_faq() 
{ 
    $labels = array(
     'name'    => __('Programme FAQ', 'text_domain'), 
     'singular_name'  => __('single programme faq', 'text_domain'), 
     'add_new'   => _x('Add Programme FAQ', '${4:Name}', 'text_domain'), 
     'add_new_item'  => __('Add Programme FAQ', 'text_domain}'), 
     'edit_item'   => __('Edit this Programme FAQ', 'text_domain'), 
     'new_item'   => __('New Programme FAQ', 'text_domain'), 
     'view_item'   => __('View Programme FAQ', 'text_domain'), 
     'search_items'  => __('Search Programme FAQ', 'text_domain'), 
     'not_found'   => __('No Programme FAQs found', 'text_domain'), 
     'not_found_in_trash' => __('No Programmes FAQs found in Trash', 'text_domain'), 
     'parent_item_colon' => __('Parent single post type name:', 'text_domain'), 
     'menu_name'   => __('Programme FAQ', 'text_domain'), 
    ); 

    $args = array(
     'labels'    => $labels, 
     'hierarchical'  => false, 
     'description'   => 'description', 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_menu'  => true, 
     'menu_position'  => 5, 
     'show_in_nav_menus' => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'has_archive'   => true, 
     'query_var'   => true, 
     'can_export'   => true, 
     'rewrite'    => true, 
     'capability_type'  => 'page', 
     'supports'   => array(
      'title', 
      'editor', 
      'author', 
      'page-attributes', 
      'thumbnail', 
      'excerpt', 
      'custom-fields', 
      'revisions', 
      'comments' 
     ), 
     'rewrite' => array(
      'with_front' => false, 
      'slug'  => 'programme_faq' 
     ) 
    ); 
    register_post_type('programme_faq', $args); 
} 
add_action('init', 'register_cpt_programme_faq'); 

// 2a. meta box - add a custom meta box on the Programme FAQ edit page 
function pfaq_add_meta_boxes($post) 
{ 
    add_meta_box('pfaq-parent', 'Parent Programme', 'pfaq_parent_meta_box', $post->post_type, 'side', 'core'); 
} 
add_action('add_meta_boxes_programme_faq', 'pfaq_add_meta_boxes'); 

// 2b. select box - add a select input inside the Parent Programme meta box 
function pfaq_parent_meta_box($post) 
{ 
    $parents = get_posts(
     array(
      'post_type' => 'programmes', 
      'orderby'  => 'title', 
      'order'  => 'ASC', 
      'numberposts' => -1 
     ) 
    ); 

    if(!empty($parents)) 
    { 
     echo '<select name="parent_id" class="widefat">'; 

     foreach($parents as $parent) 
     { 
      printf('<option value="%s"%s>%s</option>', esc_attr($parent->ID), selected($parent->ID, $post->post_parent, false), esc_html($parent->post_title)); 
     } 

     echo '</select>'; 
    } 
} 

// 3. rewrite rule - teach WordPress to parse the custom URL pattern 
function pfaq_rewrite_rule() 
{ 
    add_rewrite_rule('^programmes/([^/]+)/faq/?', 'index.php?programme_faq=$matches[1]-faq', 'top'); 
} 
add_action('init', 'pfaq_rewrite_rule'); 

// 4. permalink - filter all Programme FAQ permalinks to match the desired URL pattern 
function pfaq_post_type_link($post_link, $post, $leavename, $sample) 
{ 
    if($post->post_type == 'programme_faq') 
    { 
     $parent = get_post($post->post_parent); 
     $post_link = get_bloginfo('url') . '/programmes/' . $parent->post_name . '/faq'; 
    } 

    return $post_link; 
} 
add_filter('post_type_link', 'pfaq_post_type_link', 1, 4); 

注:

我已經重複的代碼從原來的問題,反映了幾個重要的變化:

  • 解決方案需要非等級文章類型
  • 改名功能爲了一致性
  • 元框代碼中的糾正問題
  • 糾正「程序」與功能受到影響的「程序」

前綴「pfaq」被選爲大多數圍繞「程序常見問題」帖子類型的自定義項。

鳴謝:

WordPress的核心,WordPress的法典,@justintadlock,@tareq_cse

相關問題