在文章页面, 如果我们想列出当前目录下的其他文章, 或者说所有文章
我们可以使用两个步骤实现, ①获取当前目录id, ②列出指定id下的所有文章
get_the_category
使用 get_the_category() 函数获取当前文章所在的分类目录
<?php $category = get_the_category($id); ?>
其中 $id(可选)文章的ID编号,默认值为$post->ID (当前文章的ID编号)。
其他参数
- cat_ID ==> 分类ID编号 (也可以保存为 ‘term_id’)
- cat_name ==> 类别名称 (也可以保存为 ‘name’)
- category_nicename ==> 类分类名称产生的一个slug (也可以保存为’slug’)
- category_描述 ==> 分类描述 (也保存为’description’)
- category_parent ==> 当前分类的父类别的ID编号。’0′ 表示没有父类别。(同样也可以保存为 ‘parent’)
- category_count ==> 该分类有多少文章 (也可以保存为 ‘count’)
query_posts
使用 query_posts() 函数实现查询某个分类下的文章
<?php
query_posts('$args');?>
常用参数
- cat ==> 分类目录id
- category_name ==> 分类目录名称
- tag ==> 标签名
- author_name ==> 作者名
- author ==> 作者id
- p ==> 文章id
- name ==> 文章名
- page_id ==> 页面id
- pagename ==> 页面名
- showposts ==> 显示文章的数量
- hour ==> 发表时间的hour
- minute ==> 发表时间的minute
- second ==> 发表时间的second
- day ==> 发表时间的day
- monthnum ==> 发表时间的month
- year ==> 发表时间的year
- order ==> 对文章排序
代码实现
这里需要了解一下其他的东西
- query, 它是临时存储查询到的数据, 用完后给它reset()
- 当query中有数据时, 使用 have_posts() 函数判断
<!--循环输出当前分类,以及当前分类下的所有文章-->
<section>
<!--获取当前文章所在的目录id--><?php foreach((get_the_category()) as $category)
{
$catid = $category->cat_ID;
}
?>
<!--输出分类的标题 --><h2><?php echo "分类:"; ?> <?php wp_list_categories("include=$catid &title_li=&style=none"); ?> </h2>
<!--输出分类的描述 --><?php echo category_description($catid); ?>
<!-- //query_posts 给 The Loop 限定的条件是:显示所有分类文章 --><?php query_posts("order=asc&showposts=-1&cat= $catid"); ?>
<!--//The Loop 开始循环把每篇文章以li嵌套<a>标签打印出来 -->
<ol>
<?php while (have_posts()) : the_post(); ?>
<li >
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><? echo get_the_title(); ?></a>
</li >
<?php endwhile;wp_reset_query();
?>
</ol>
</section>