wordpress 获取当前 single.php archive.php 分类url的位置及方法

标签:
来源: 老季博客
日期: 2016-1-19
作者: 腾讯云/服务器VPS推荐评测/Vultr
阅读数: 55

我们在wordpress自行编写主题时没有现成的方法,现在我们自己弄个方法。

步骤如下:

1、在functions.php 里面添加下面2个方法:

function get_category_root($cat){
    $this_category = get_category($cat);
    while($this_category->category_parent){
        $this_category = get_category($this_category->category_parent);
        return $this_category;
}
function get_article_category_ID(){
    $category = get_the_category();
    return $category[0]->cat_ID;
}

2、使用时就是:

<a href="<?php echo esc_url(home_url());?>">首页</a> <?php $jpostion = get_category_root(get_article_category_ID()); echo '<a href="'.esc_url(get_category_link($jpostion->term_id)).'" >'.$jpostion->name.'</a>'?>
链接到文章: https://jiloc.com/30086.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注