获取文章目录使用get_categories,参考文档:http://codex.wordpress.org.cn/Function_Reference/get_categories
获取文章目录的链接使用get_category_link,参考文档:http://codex.wordpress.org.cn/Function_Reference/get_category_link
下面的代码片段演示了如何获得文章顶级目录并及其链接:
function get_root_categories()
{
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false );
$categories = get_categories( $args );
foreach ($categories as $cat)
{
echo "<li><a href=".get_category_link($cat->term_id).">".$cat->name."</a></li>";
}
}
还可以通过默认的模板函数wp_list_categories生成目录列表,参数文档:http://codex.wordpress.org.cn/Template_Tags/wp_list_categories
以下为示例代码:
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list',
'show_count' => 1,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '',
'exclude_tree' => '',
'include' => '',
'hierarchical' => 1,
'title_li' => __( 'Categories' ),
'show_option_none' => __('No categories'),
'number' => null,
'echo' => 1,
'depth' => 1,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => null
);
wp_list_categories( $args );