我刚刚在帖子类型“短片”和帖子类型“卡通系列”之间建立了帖子/父级关系。
我使用以下代码在meta框中添加内容,以从另一种帖子类型分配父项:
add_action('admin_menu', function() {
remove_meta_box('pageparentdiv', 'episodes', 'normal');
});
add_action('add_meta_boxes', function() {
add_meta_box('episodes-parent', 'Cartoon Series', 'episodes_attributes_meta_box', 'episodes', 'side', 'default');
});
function episodes_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' => 'cartoon-series', '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.
}
这在管理屏幕上起作用,允许我将系列设置为该集的父级,但是当我尝试查看该帖子时,我得到了404。URL结构为:
domain/episodes/series-name/episode-name
该系列的网址是:
domain/cartoon-series/series-name
我希望该集的网址为:
domain/cartoon-series/series-name/episode-name
我想念什么?是否可以使整个帖子类型成为其他帖子类型的子类型?因此,我什至可以获取剧集列表的网址为:
domain/cartoon-series/series-name/episodes
谢谢!马特
根据要求,以下是有关两种自定义帖子类型的代码:
$labels = array(
"name" => "Cartoon Series",
"singular_name" => "Cartoon Series",
"menu_name" => "Cartoon Series",
"all_items" => "All Cartoon Series",
"add_new" => "Add New",
"add_new_item" => "Add New Cartoon Series",
"edit" => "Edit",
"edit_item" => "Edit Cartoon Series",
"new_item" => "New Cartoon Series",
"view" => "View",
"view_item" => "View Cartoon Series",
"search_items" => "Search Cartoon Series",
"not_found" => "No Cartoon Series Found",
"not_found_in_trash" => "No Cartoon Series Found in Trash",
"parent" => "Parent Cartoon Series",
);
$args = array(
"labels" => $labels,
"description" => "",
"public" => true,
"show_ui" => true,
"has_archive" => true,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "cartoon-series", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "revisions", "thumbnail" ), );
register_post_type( "cartoon-series", $args );
$labels = array(
"name" => "Episodes",
"singular_name" => "Episode",
);
$args = array(
"labels" => $labels,
"description" => "",
"public" => true,
"show_ui" => true,
"has_archive" => true,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "episodes", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "revisions", "thumbnail" ), );
register_post_type( "episodes", $args );
我正在使用CPT UI插件,因此无法直接编辑该代码。那只是CPT UI提供的导出代码。
我没有任何其他代码可以链接这两个CPT。也许那就是我所缺少的。我刚刚在网上找到了将metabox放在页面上进行链接的代码。做这项工作还不够吗?看起来它设置了post_parent。
谢谢!马特
pre_get_posts
更改查询而没有成功,但其中涉及到一些我还无法理解的复杂问题。就像让猫成为狗的父母一样。我建议仅使用一种分层的帖子类型,或使用meta字段设置realtionship 。
parent_id
查询var,不需要更多代码来设置关系。问题是WordPress无法识别生成的URL。我一直在试图找到一个使它起作用的重写规则,但是我没有成功。我正在研究一个解决方案。