the_category()函数使用方法解析

来自:互联网
时间:2020-03-03
阅读:

我们知道是wordpress调用分类的函数,它和the_tags()调用tag标签有点类似,怎么使用呢?随ytkah一起来看看

<?php the_category( $separator, $parents, $post_id ); ?>

$separator 指定间隔符号;

$parents 分类显示方式,两个值multiple和single;multiple值显示所有分类,single只显示指定文章ID的父分类:最后级分类。

$post_id 文章的ID号;

举个例子:在文章页中调用当前分类

<span>Category: <?php the_category(' > ', 'single'); ?> |</span>
<span><?php the_time('M.d.Y');?> | <?php the_tags( 'Tags: ', ', ', '' ); ?></span>

显示效果如下

当然你也可以输出不带链接的分类名称

<?php foreach((get_the_category()) as $category){
echo $category->name."<br>";
}?>

还可以这样调用

<?php foreach((get_the_category()) as $category){
                echo '<a href="' . get_category_link( $category->term_id ) . '">' .$category->name.'</a>';
            }?>

参考资料https://developer.wordpress.org/reference/functions/get_categories/

返回顶部
顶部