wordpress获取调用当前分类名称及别名
在wordpress主题页面里怎样获取文章所属分类的名字呢?开发wordpress主题时分类页面及文章页面几乎都会用到调用当前分类及文章所谓分类名称的需求,今天分享给大家
文章列表里调用分类名称及链接。
<?php the_category(); ?>
缺点是不能单独调用出分类名称。
在分类页面调用当前分类名称
则使用以下代码:
<?php single_cat_title(); ?>
在文章页想调用出当前文章所属分类的名称:
推荐大家三组代码,
<?php foreach((get_the_category()) as $category){ echo $category->cat_name;}?>
<?php $category = get_the_category(); echo $category[0]->cat_name; ?>
<?php $category = get_the_category(); echo $category[0]->cat_name; ?>
<?php $thiscat = get_category($cat); echo $thiscat ->name;?>
<?php $thiscat = get_category($cat); echo $thiscat ->name;?>
如何获取分类别名的方法
if(is_category()) { $cat = get_query_var('cat'); $yourcat = get_category($cat); echo "该分类别名为" . $yourcat->slug; }
写笔记