本文实例讲述了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实现获取父类分类名称的方法》的一些看法。更多内容请查看本栏目更多内容!
自定义 WordPress 默许评论头像 关于没有设置Gravatra头像的评论者来说,WordPress会显示一个你在后盾设置的...
此时假如上传一个不在预约义的平安扩大名列表,如.lrc,会报错: File type does not meet security guidel...
get_template_part() 用来援用模板文件,相似于 get_header()、get_sidebar() 和 get_footer(),只不过这个...
本文是简略易懂的古代魔法系列文章的第二弹~ 一、Flowplayer简介 FlowPlayer 是一个用Flash开发的在Web上的...
第一种办法:我的这个博客不断都在网上运转,明天想把它在本地架设一个,包括数据库都和网上的如出一辙。详...
如在 Nicky 的博客中, 我发现了下图中的搜寻栏. 在搜寻栏中增加这样的提醒信息, 能够疏导访客对网站进行搜...