欢迎光临
我们一直在努力

wordpress子分类调用父分类名称和链接

专为导航而生,在wordpress模板制作过程中常常会在做breadcrumbs导航时会用到,子分类调用父分类的名称和链接,下面这段简洁的代码,可以完美解决这个问题。

<?php echo get_category_parents( $cat, true, ' &raquo; ' ); ?>

下面这种方法也可以,不过代码不够简洁。

<?php
if ( is_category() ) {
    // Get the current category term id.
    $query_obj = get_queried_object();
    $term_id   = $query_obj->term_id;
 
    echo get_term_parents_list( $term_id, 'category' );
}
?>

第三种方法,调用分类目录名称和链接,作为导航。

<?php
if ( ( is_tax() || is_category() || is_tag() ) ) {
    $trail     = '';
    $home      = '/<a href="' . get_home_url() . '">Home</a>';
    $query_obj = get_queried_object();
    $term_id   = $query_obj->term_id;
    $taxonomy  = get_taxonomy( $query_obj->taxonomy );
 
    if ( $term_id && $taxonomy ) {
        // Add taxonomy label name to the trail.
       // $trail .=  '/' . $taxonomy->labels->menu_name;
        // Add term parents to the trail.
        $trail .= '/' . get_term_parents_list( $term_id, $taxonomy->name, array( 'inclusive' => false ) );
    }
 
    // Print trail and add current term name at the end.
    echo '<p class="breadcrumb-trail">' . $home . $trail . $query_obj->name . '</p>';
}
?>

 

赞(0) 打赏
未经允许不得转载:新起点博客 » wordpress子分类调用父分类名称和链接


关注公众号『新起点软件管家』

获取最新网络资源及破解软件!
带你玩转各样软件...

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏