我目前正在使用以下代码显示一个列表,其中包含指向特定CPT和分类中帖子的链接:
<?php
$custom_terms = get_terms(\'videoscategory\');
foreach(array($custom_terms as $custom_term) {
wp_reset_query();
$args = array(\'post_type\' => \'product\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'videoscategory\',
\'field\' => \'slug\',
\'terms\' => $custom_term->slug
),
array(
\'taxonomy\' => \'product_category\',
\'field\' => \'slug\',
\'terms\' => $other_custom_term->slug
),
)
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo \'<h1 style="margin-top:10px;">\'.$custom_term->name.\'</h1>\';
while($loop->have_posts()) : $loop->the_post();
echo \'<h2><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></h2>\';
endwhile;
}
} ?>
它工作正常,但我只想显示两种分类法中的帖子。要这样做,我需要补充什么?
非常感谢您的帮助,谢谢。
最合适的回答,由SO网友:730wavy 整理而成
好吧,我想出来了!
<?php
$custom_terms = get_terms(\'your_other_category\');
$other_custom_terms = get_terms(\'your_category\');
foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
wp_reset_query();
$args = array(\'post_type\' => \'product\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'your_category\',
\'field\' => \'slug\',
\'terms\' => $other_custom_term->slug
),
array(
\'taxonomy\' => \'your_other_category\',
\'field\' => \'slug\',
\'terms\' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo \'<h1 style="margin-top:10px;">\'.$custom_term->name.\'</h1>\';
while($loop->have_posts()) : $loop->the_post();
echo \'<h2><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></h2>\';
endwhile;
}
}
} ?>
SO网友:montrealist
根据the Codex, 以下是您如何从多个分类中查询帖子:
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'videoscategory\',
\'field\' => \'slug\',
\'terms\' => $custom_term->slug
),
array(
\'taxonomy\' => \'yourothertaxonomy\',
\'field\' => \'slug\',
\'terms\' => $other_custom_term->slug
)
)