小冬SEO

wordpress调用当前分类名称和链接方法

2019-08-12 1:14:45 3000 WordPress笔记

wordpress在调用文档列表的时候,我们需要获取某篇文档所属分类名称和分类链接,需要用到相关的调用代码,一下是我整理的可用的代码:

调用分类名称和链接:

<?php the_category(); ?>

调用分类名称:

<?php foreach((get_the_category()) as $category){echo $category->cat_name;} ?>
或
<?php the_category(', ') ?>

调用分类别名方法:

<?php $cat = get_category($cid);echo $cat->slug;?>
或
<?php if(is_category()) { $cat = get_query_var('cat'); $yourcat = get_category($cat); echo "" . $yourcat->slug; }?>

调用链接方法:

<?php

$category = get_the_category();

if($category[0]){

echo ''.get_category_link($category[0]->term_id ).'';

}

?>

单独调用分类名称:

<?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 $thiscat = get_category($cat); echo $thiscat ->name;?>

调用分类别名方法:

if(is_category()) {

$cat = get_query_var('cat');

$yourcat = get_category($cat);

echo "该分类别名为" . $yourcat->slug;

}

或者:

<?php $cat = get_category($cid);echo $cat->slug;?>  

分类页面调用代码:

<?php if(is_category()) {$cat = get_query_var('cat');$yourcat = get_category($cat);echo "" . $yourcat->slug;} ?>  

page页面调用分类:

<?php if( is_page() ){$content = $content . get_option('display_copyright_text');$post_data = get_post($post->ID, ARRAY_A); echo $slug = $post_data['post_name'];} ?>  

版权保护: 本文由小冬SEO编辑发布,转载请保留链接: https://www.cdseoyh.cn/shuo/109.html

小冬SEO 草根seoer,从事搜索引擎关键词优化,喜欢学习专研各类SEO优化技巧—小冬SEO博客
  • 138文章总数
  • 3001本页访问
  • 运营时间
  • 标签

    友情链接

      {dede:sql sql="select url,webname from dede_flink where typeid = (select id from dede_flinktype where typename=~typename~)"}
    • [field:webname/]
    • {/dede:sql}