我三天没找到正确的代码。。。现在我问你:
我希望实现以下目标:
这里是我的帖子查询:
$posts = new WP_Query(array(
\'post_type\' => \'tarife\',
\'posts_per_page\' => 30,
\'post_status\' => \'publish\',
\'post__in\' => $products
));
如果
term 我的习惯分类法[tarife]是\'
info\'
比显示loop 1
while($posts->have_posts()):
$posts->the_post();
$response .= \'loop1\';
endwhile;
如果
term 我的习惯分类法[tarife]是\'
glasfaser\'
比显示loop 2
while($posts->have_posts()):
$posts->the_post();
$response .= \'loop2\';
endwhile;
<小时>
EDIT:
现在我已经这样尝试过了,但如果存在一个分类法,它会显示两个循环。
$products = self::$productDefinitions[$result[\'Standorttyp\']];
$posts_info = new WP_Query(array(
\'post_type\' => \'tarife\',
\'posts_per_page\' => 30,
\'post_status\' => \'publish\',
\'post__in\' => $products,
\'taxonomy\' => \'info\'
));
$posts_gf = new WP_Query(array(
\'post_type\' => \'tarife\',
\'posts_per_page\' => 30,
\'post_status\' => \'publish\',
\'post__in\' => $products,
\'taxonomy\' => \'glasfaser\'
));
if ( $posts_info->have_posts() ) :
while ( $posts_info->have_posts() ) : $posts_info->the_post();
$response .= \'infos\';
endwhile;
wp_reset_postdata();
endif;
if ( $posts_gf->have_posts() ) :
while ( $posts_gf->have_posts() ) : $posts_gf->the_post();
$response .= \'tarife\';
endwhile;
wp_reset_postdata();
endif;
如果我在前端选择选项1,结果应该是:tarife tarife tarife
如果我在前端选择选项2,结果应该是:info
我得到的:
选项1:info info info tarife tarife tarife
选项2:信息tarife
SO网友:Lara
已找到解决方案
我只需添加正确的tax\\u查询。。。
$posts_info = new WP_Query(array(
\'post_type\' => \'tarife\',
\'posts_per_page\' => 3,
\'post_status\' => \'publish\',
\'post__in\' => $products,
\'orderby\' => \'title\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'tarif_kategorie\',
\'field\' => \'slug\',
\'terms\' => \'info\',
),
),
));
$posts_gf = new WP_Query(array(
\'post_type\' => \'tarife\',
\'posts_per_page\' => 5,
\'post_status\' => \'publish\',
\'post__in\' => $products,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'tarif_kategorie\',
\'field\' => \'slug\',
\'terms\' => \'glasfaser\',
),
),
));