用法一、查询某一分类下的文章,代码如下:
<?php
$query = new WP_Query([
'post_type' => 'product', // 文章类型是自定义类型 product
'posts_per_page' => 999, // 每页 999 条
'order' => 'ASC', // 升序排序
// 查询分类,即该自定义类型下的分类
'tax_query' => [
[
// 分类的名字
'taxonomy' => 'product_category',
// 根据wp_terms表里的哪个字段进行查询,可以根据term_id或slug等
'field' => 'term_id',
// 上面字段的值
'terms' => $sub->term_id,
]
],
]);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
?>
<a href="<?php the_permalink() ?>"><?php the_title() ?></a>
<?php
endwhile;
endif;
// 使用 WP_Query 查询后,不要忘了进行重置查询,否则会影响后面代码的执行
wp_reset_query();
?>
发表回复