您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
22. 获取文章所属的分类目录信息(纯数据版)
发布时间:2023-03-14 16:35:11编辑:雪饮阅读()
基于上篇,这次其实主要是了解下:
某文章所属分类目录信息的纯数据版(html元素返回不直接输出)
某文章所属分类目录信息的纯数据版(数组返回,不直接输出)
某文章所属目录的目录归档页网址的两种获取方法
则当前使用主题目录中的single.php如:
<?php
/*
get_the_category_list与上篇中the_category类同,只是这个仅仅返回值(字符串的分类列表,是html元素),需要你自己用echo而已
get_the_category相当于是the_category的纯数据版,返回的是一个数组
*/
?>
<?php if(have_posts()):?>
<?php while(have_posts()):the_post();?>
<div style="border:1px solid pink;">
<div style="display:flex;justify-content: center;">
<div style="border:1px solid red; width:90%;">
<p>文章所属分类信息 get_the_category_list:<?php echo get_the_category_list();?></p>
<p>文章所属分类信息 get_the_category:<?php print_r(get_the_category());?></p>
<p>文章所属分类信息 get_the_category(遍历):<?php $cats=get_the_category();?></p>
<?php foreach($cats as $cat):?>
<p style="text-indent:50px;">编号:<?php echo $cat->term_id;?></p>
<p style="text-indent:50px;">名称:<?php echo $cat->name;?></p>
<p style="text-indent:50px;">别名:<?php echo $cat->slug;?></p>
<p style="text-indent:50px;">描述:<?php echo $cat->description;?></p>
<p style="text-indent:50px;">分类归档页网址:<?php echo get_category_link($cat);?></p>
<p style="text-indent:50px;">分类归档页网址(仅传分类id):<?php echo get_category_link($cat->term_id);?></p>
<?php endforeach;?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<p>对不起,暂时没有任何内容!</p>
<?php endif;?>
关键字词:分类,目录,纯数据