本文实例讲述了wordpress完成获取父类分类称号的办法。分享给大家供大家参考。详细剖析如下:
在wordpress中获取父类分类名的办法说法有很多种,如:获取以后ID父类名等等这里就不引见了,上面我来引见几种获取父类名字函数,WordPress中获取父分类的办法,能够用在single,category中,有父分类显示父分类没有显示以后分类.
实例代码如下:
<?php
$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!emptyempty($parent)) {
echo $parent;
} else {
echo $category[0]->cat_name;
}
?>假如分类ID是固定的,用就能够完成,然而假如分类ID不是确定的,而是心愿主动绑定各分类,这个函数就不能满足了,需求先获取到以后页面的根分类ID,代码如下:
function get_category_root_id($cat)
{
$this_category = get_category($cat); // 获得以后分类
while($this_category->category_parent) // 若以后分类有下级分类时,循环
{
$this_category = get_category($this_category->category_parent); // 将以后分类设为下级分类(往上爬)
}
return $this_category->term_id; // 前往根分类的id号
}wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=");<?php
$category = get_the_category();
$cat_parent = get_cat_name($category[0]->category_parent);
if (!emptyempty($cat_parent)) {
echo $cat_parent;
} else {
echo $category[0]->cat_name;
}
?>get_category_parents()函数的性能是前往当然分类的父分类列表,当然也蕴含了以后分类,这个函数齐全是为导航而生.
【函数用法】代码如下:
<?php echo get_category_parents($category, $display_link, $separator, $nice_name); ?>$category
(integer) 分类ID,默许为以后分类ID
$display_link
(boolean) 能否创立更分类的链接.
$separator
(string) 各个分类的分隔符.
$nice name
(boolean) 能否前往分类别名 (默许为:FALSE,不前往).
【示例】
指定不同的目录文章使用不同的模板,输入以后分类的父级分类,分隔符为 ‘»’
实例代码如下:
<?php echo get_category_parents($cat, TRUE, ' » '); ?>
输入:Internet » Blogging » WordPress »
心愿本文所述对大家的WordPress建站有所协助。
以上就是安达网络工作室关于《wordpress实现获取父类分类名称的方法》的一些看法。更多内容请查看本栏目更多内容!
作者 查问和某些作者(用户)无关的文章,能够应用 4 个参数: author(整数):用户 ID author_name(...
WordPress网站在改换域名后,需求从新配置以后域名能力使得网站失常运转,很多人遇到过这样的状况,那么如何...
我想这对沙发同窗不太偏心吧。昨天在看yinheli同窗的《鼠标悬浮完成显示留言内容》一文(倡议看看,好文章)...
【阐明】 反省以后文章能否置顶。前往值TRUE 或许 FALSE. 【用法】 复制代码代码如下:<?php is_sticky($pos...
get_post() 在普通主题制造时,get_post()函数咱们普通很少会用到,但由于前面会讲到get_posts(),所以咱们...
WP的性能弱小在于有支持并完满兼容的插件。自身WP链接治理不好自在排序,CoCo Link Sort 这款插件补偿了这样...