我将我的自定义帖子类型设置为“层次结构”(Hierarchy),url的工作方式如下:狮子新闻(Lions News)>S1›测试帖子1(Test post 1),这很好,只是它应该更进一步,就像狮子新闻(Lions Mini)>狮子新闻(Lions News)>S1›测试帖子1(Lions Mini)是所有内容的父页面。有没有办法设置?
以下是我在函数中的设置。php
/** Custom Post Types for Lions News */
$labels = array(
\'name\' => \'Lions News Categories\',
\'singular_name\' => \'Lions Category\',
\'search_items\' => \'Search Lions Categories\',
\'popular_items\' => \'Popular Lions Categories\',
\'all_items\' => \'All Lions Categories\',
\'parent_item\' => \'Parent Lions Category\',
\'edit_item\' => \'Edit Lions Category\',
\'update_item\' => \'Update Lions Category\',
\'add_new_item\' => \'Add New Lions Category\',
\'new_item_name\' => \'New Lions Category\',
\'separate_items_with_commas\' => \'Separate Lions Categories with commas\',
\'add_or_remove_items\' => \'Add or remove Lions Categories\',
\'choose_from_most_used\' => \'Choose from most used Lions Categories\'
);
$args = array(
\'label\' => \'Lions Categories\',
\'labels\' => $labels,
\'public\' => true,
\'hierarchical\' => true,
\'show_ui\' => true,
\'show_in_nav_menus\' => true,
\'args\' => array( \'orderby\' => \'term_order\' ),
\'rewrite\' => array( \'slug\' => \'lions_news/lions_articles\', \'with_front\' => false ),
\'query_var\' => true
);
register_taxonomy( \'lions_articles\', \'lions_news\', $args );
register_post_type( \'lions_news\',
array(
\'labels\' => array(
\'name\' => __( \'Lions News\' ),
\'singular_name\' => __( \'Lions News\' )
),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'supports\' => array( \'title\',\'editor\',\'thumbnail\',\'comments\',\'revisions\' ),
\'rewrite\' => array( \'slug\' => \'lions_news\', \'with_front\' => false ),
\'has_archive\' => true
)
);
/** End custom Post Types */
谢谢!
SO网友:Steve82
如果您在register\\u post\\u type中将hierarchy设置为false,那么下面的代码可以工作(我的理解是,您实际上不需要新闻帖子之间的层次关系,只需要它们与页面层次结构相关联)。
以下内容在此基础上进行了提升和修改:http://justintadlock.com/archives/2013/10/07/post-relationships-parent-to-child
/* Hook meta box to just the \'lions_news\' post type. */
add_action( \'add_meta_boxes_lions_news\', \'my_add_meta_boxes\' );
/* Creates the meta box. */
function my_add_meta_boxes( $post ) {
add_meta_box(
\'my-lions-news-parent\',
\'Parent Page\',
\'my_lions_news_parent_meta_box\',
$post->post_type,
\'side\',
\'core\'
);
}
/* Displays the meta box. */
function my_lions_news_parent_meta_box ( $post ) {
$parents = get_pages(
array(
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
\'sort_column\' => \'menu_order\',
\'parent\' => -1
)
);
if ( !empty( $parents ) ) {
echo \'<select name="parent_id" class="widefat">\'; // !Important! Don\'t change the \'parent_id\' name attribute.
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>\';
}
}
您可以更换
\'parent\' => -1
在具有任何页面id的get\\u页面中,将下拉列表限制为仅显示该页面的子级。
最简单的方法可能是为分配了新闻文章的所有页面创建一个页面模板,该模板通过当前页面的子帖子进行自定义循环。