wordpress 如何按类别 slug 查询帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19792479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to query_posts by category slug
提问by user1845661
I am using the following code to list some pages on my wordpress site:
我正在使用以下代码列出我的 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 -->
I am wondering if there would be a way to limit those based on a certain category slug? Thanks in advance
我想知道是否有办法限制基于某个类别 slug 的那些?提前致谢
回答by Adnan
tax_query is used to get the posts associated with certain taxonomy.
tax_query 用于获取与特定分类法相关的帖子。
- {tax}(string) - use taxonomy slug. Deprecatedas of Version 3.1 in favor of 'tax_query'.
- tax_query(array) - use taxonomy parameters (available with Version 3.1).
- taxonomy(string) - Taxonomy.
- field(string) - Select taxonomy term by ('id' or 'slug')
- terms(int/string/array) - Taxonomy term(s).
- include_children(boolean) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
- operator(string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
- {tax}( string) - 使用分类 slug。自 3.1 版起弃用,支持“tax_query”。
- tax_query( array) - 使用分类参数(在 3.1 版中可用)。
- 分类法(字符串)- 分类法。
- field( string) - 通过 ('id' 或 'slug') 选择分类术语
- 术语( int/string/array) - 分类术语。
- include_children( boolean) - 是否包含层次分类法的子项。默认为真。
- operator( string) - 要测试的运算符。可能的值为“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
}
}
回答by Jean
You can use the variable category_name
like this:
您可以category_name
像这样使用变量:
$args = array( 'category_name' => ***YOUR CATEGORY SLUG***, 'posts_per_page' => 12, 'order'=> 'ASC', 'post_type' => 'tcp_product', 'paged' => $paged);