WordPress的自定义文章类型不能访问子页
问题描述:
下面的成功让我使用程序和计划常见问题解答在我的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.
}
答
这是我第一次回答堆栈溢出。请原谅格式和拼写问题。
内容:
在到达一个解决方案的关键步骤是记录在代码。这是一个总结:
- 注册自定义文章类型
- 添加一个“父计划”的编辑屏幕
- 在元框添加重写规则,以便WordPress的正确分析自定义URL结构
- 过滤器在永久所以链接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