WordPress获取分类文章的函数有query_posts和WP_Query,两个函数的功能都很强大,推荐使用WP_Query函数来查询文章。对于默认的文章类型可以使用cat(整数)、category_name(字符串)、category__and(数组)、category__in(数组)来获取分类下的文章,而获取自定义文章类型下分类的文章则需要通过自定义分类法来查询。
直接贴出代码,其中的注释一看就能明白:
<!--自定义文章类型分类查询-->
<?php $salong_posts = new WP_Query(
array(
'post_type' => 'video',//自定义文章类型,这里为video
'ignore_sticky_posts' => 1,//忽略置顶文章
'posts_per_page' => 6,//显示的文章数量
'tax_query' => array(
array(
'taxonomy' => 'video_category',//分类法名称
'field' => 'id',//根据分类法条款的什么字段查询,这里设置为ID
'terms' => 1,//分类法条款,输入分类的ID,多个ID使用数组:array(1,2)
)
),
)
);
?>
<ul>
<?php if ($salong_posts->have_posts()): while ($salong_posts->have_posts()): $salong_posts->the_post(); ?>
<!-- 文章 -->
<li>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</li>
<!-- 文章end -->
<?php endwhile; endif; ?>
</ul>

