我只想列出按各自流派(使用下面的代码进行分类)组织的书籍(自定义帖子类型)。我有下面列出的所有正在使用的代码。
我在页面模板中使用以下代码来调用自定义帖子类型“book”:
<?php
if ( get_query_var(\'paged\') ) { $paged = get_query_var(\'paged\'); }
elseif ( get_query_var(\'page\') ) { $paged = get_query_var(\'page\'); }
else { $paged = 1; }
$myposts = array(
\'post_type\' => \'book\',
\'posts_per_page\' => \'40\',
\'orderby\' => \'id\',
\'order\' => \'ASC\',
\'paged\'=> $paged
);
query_posts($myposts);
?>
这是函数中的自定义post类型原点。php文件:
add_action( \'init\', \'register_cpt_book\' );
function register_cpt_book() {
$labels = array(
\'name\' => _x( \'Books\', \'book\' ),
\'singular_name\' => _x( \'Book\', \'book\' ),
\'add_new\' => _x( \'Add New\', \'book\' ),
\'add_new_item\' => _x( \'Add New Book\', \'book\' ),
\'edit_item\' => _x( \'Edit Book\', \'book\' ),
\'new_item\' => _x( \'New Book\', \'book\' ),
\'view_item\' => _x( \'View Book\', \'book\' ),
\'search_items\' => _x( \'Search Books\', \'book\' ),
\'not_found\' => _x( \'No books found\', \'book\' ),
\'not_found_in_trash\' => _x( \'No books found in Trash\', \'book\' ),
\'parent_item_colon\' => _x( \'Parent Book:\', \'book\' ),
\'menu_name\' => _x( \'Books\', \'book\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'description\' => \'A collection of books\',
\'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'custom-fields\' ),
\'taxonomies\' => array( \'author\', \'genre\' ),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'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\' => \'post\'
);
register_post_type( \'book\', $args );
}
下面是函数中的自定义分类“流派”。php:
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
\'name\' => _x( \'Genres\', \'genre\' ),
\'singular_name\' => _x( \'Genre\', \'genre\' ),
\'search_items\' => __( \'Search Genres\' ),
\'all_items\' => __( \'All Genres\' ),
\'parent_item\' => __( \'Parent Genre\' ),
\'parent_item_colon\' => __( \'Parent Genre:\' ),
\'edit_item\' => __( \'Edit Genre\' ),
\'update_item\' => __( \'Update Genre\' ),
\'add_new_item\' => __( \'Add New Genre\' ),
\'new_item_name\' => __( \'New Genre Name\' ),
\'menu_name\' => __( \'Genre\' ),
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'genre\' ),
);
register_taxonomy( \'genre\', array( \'book\' ), $args );
代码工作得很好。正如我所说,我只想列出按各自流派组织的书籍(上述分类法)。
因此,书籍循环将如下所示:
Philosophy
哲学书1、哲学书2、哲学书3
Psychology
心理学第一册,心理学第二册,心理学第三册
History
历史书1、历史书2、历史书3
我在谷歌搜索时尝试了几种方法,但都没有找到任何有效的方法。如果有人能给我一个可行的答案,我将不胜感激。谢谢