下面的成功讓我使用程序和計劃常見問題解答在我的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.
}