我正在尝试通过标签ID使帖子的自定义类型循环。
这是原始代码:
<?php $args = array(
'post_type' => 'kana_portfolio', 'meta_key' => 'choose_layout_2', 'meta_value' => 'layout-1', 'posts_per_page' => -1
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
因为我只需要循环ID为31的特定类别(自定义帖子类型)中的帖子, 所以我在”‘posts_per_page’=> -1″之后添加了”‘tag_id’=> 31″
<?php $args = array(
'post_type' => 'kana_portfolio', 'meta_key' => 'choose_layout_2', 'meta_value' => 'layout-1', 'posts_per_page' => -1, 'tag_id' => 31
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
[编辑]
我在页面下面有另一个循环, 我尝试插入’cat’=> 31 tax_query数组
<?php $number_posts_to_display = get_field('number_of_posts_to_display');
$display_order = get_field('post_display_order');
$portfolio = array(
'post_type' => 'kana_portfolio', 'posts_per_page' => $number_posts_to_display, 'order' => $display_order, 'meta_key' => 'choose_layout_2', 'meta_value' => 'layout-1', 'tax_query' => array(
array(
'taxonomy' => 'kana_genre', 'field' => 'slug', 'terms' => $portfolio_term_array, ), ), );
$portfolio_loop = new WP_Query($portfolio); ?>
但是什么也没显示, 怎么办?
#1
如果你的类别(这里是分类类别的真实类别, …)是31, 则应使用’cat’=> 31, 标签是标签, 而不是类别。如果有帮助, 请告诉我。如果你尝试过滤标签, 但该标签无法正常工作, 请告诉我, 请深入了解。
玩得开心!
#2
代替:
'tag_id' => 31
尝试这个:
'tag__in' => 31
来源:
https://www.srcmini02.com/66701.html