如何按类别查询

use*_*661 0 wordpress

我正在使用以下代码在我的wordpress网站上列出一些页面:

$args = array( 'posts_per_page' => 12, 'order'=> 'ASC', 'post_type' => 'tcp_product', 'paged' => $paged);
?>
  <?php query_posts($args); ?>
  <?php while (have_posts()) : the_post(); ?>
  <a href="<?php the_permalink(); ?>" id="prod-link">
  <?php if( has_sub_field('images') ): ?>
  <?php $img = get_sub_field('image') ?>
  <img src="<?php echo $img['sizes']['product-index-pic'] ?>" />
  <?php endif; ?>
  </a>
  <?php endwhile; ?>
  <!-- #posts -->
  <div class="pagination">
    <?php posts_nav_link( ' ', '<img src="' . get_bloginfo('template_url') . '/assets/images/prev.jpg" />', '<img src="' . get_bloginfo('template_url') . '/assets/images/next.jpg" />' ); ?>
  </div>
  <!-- .pagination --> 
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一种方法可以限制某些类别的子弹?提前致谢

Adn*_*nan 6

tax_query用于获取与某些分类法相关的帖子。

  • {tax}字符串)-使用分类标准。自版本3.1起不推荐使用 “ tax_query”。
  • tax_query数组)-使用分类参数(版本3.1可用)。
    • 分类法字符串)-分类法。
    • 字段字符串)-通过('id'或'slug')选择分类术语
    • 术语int / string / array)-分类术语。
    • include_childrenboolean)-是否为分层分类法包括子级。默认为true。
    • 运算符字符串)-要测试的运算符。可能的值为“ IN”,“ NOT IN”和“ AND”。
$args = array(
    'post_type' => 'tcp_product',
    'posts_per_page' => 12,
    'tax_query' => array(
        array(
            'taxonomy' => 'tcp_product_taxonomy',
            'field' => 'slug',
            'terms' => 'your-cat-slug'
        )
     )
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post;
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以category_name像这样使用变量:

$args = array( 'category_name' => ***YOUR CATEGORY SLUG***, 'posts_per_page' => 12, 'order'=> 'ASC', 'post_type' => 'tcp_product', 'paged' => $paged);
Run Code Online (Sandbox Code Playgroud)