如果是做简单的导航列表,利用<?php echo get_category_parents($cat,true,'»'); ?>就可以了,不过这样做出来的导航样式父类和子类是一样的。那么如果想让他们分别拥有不同的样式呢?
我们可以通过get_the_title($post->post_parent)来获取其父类的名称,单页面的话我们可以通过<?php if($post->post_parent){$page=$post->post_parent;echo $page;}?>来获取父类的id
这样我们利用如下代码就可以获取父类名称及链接了:
<a href="<?php home_url(add_query_arg(array()));?>?page_id=<?php if($post->post_parent) {$page=$post->post_parent;echo $page;} ?>"><? php get_the_title($post->post_parent); ?></a>
文章页获取父类标题的代码如下:
<?php
$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
echo $parent;
} else {
echo $category[0]->cat_name;
}
?>
那么文章页获取父类和链接的代码
<a class="list_span_a" href="<?php home_url(add_query_arg(array()));?>?cat=<?php $category = get_the_category();if ($category[0]->category_parent){$page=$category[0]->category_parent; echo $page; }?>"> <?php $category = get_the_category(); $parent = get_cat_name($category[0]->category_parent); if (!empty($parent)) { echo $parent; } else { echo $category[0]->cat_name; }
?> </a>
有点长 应该可以修改,不当之处欢迎大家指出。