获取WordPress文章目录及其链接

获取文章目录使用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 );


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

螃蟹@横着走

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值