用代码打造完美的“当前位置”

之前介绍了两个显示当前位置的方式,其实就是两个不同的函数 get_the_category() 和 the_category() ,折腾出真知,单独使用两个函数都不尽人意:单独使用 get_the_category() ,如果文章属于两个分类,则文章页面只显示一个分类;单独使用 the_category() ,当某两个分类被同一篇文章“属于了”(能明白吧),那么在其中一个分类页面中同时显示两个分类,神奇。

不过不用害怕,两者结合就能完美了,精简了下,只在分类和文章页面显示,代码如下,按需修改~

<div class="localtion">
	<?php if  ( is_category() ) { ?>
		<p class="local">
		你的位置: <a href="http://ygs.im/" title="HOME">HOME</a> >> <?php $categorys = get_the_category(); $category = $categorys[0];echo(get_category_parents($category->term_id,true,' >>')); ?> 文章列表
		</p>
	<?php } ?>
	<?php if  ( is_single() ) { ?>
		<p class="local">
		你的位置: <a href="http://ygs.im/" title="HOME">HOME</a> >> <?php the_category(' & '); ?> >> <?php the_title(); ?>
		</p>
	<?php } ?>
</div>

附上以前的介绍:
在WordPress文章页显示当前位置
另一个显示WordPress当前位置的方法