查询2个分类中的帖子

时间:2013-04-03 作者:730wavy

我目前正在使用以下代码显示一个列表,其中包含指向特定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;
     }
} ?>
它工作正常,但我只想显示两种分类法中的帖子。要这样做,我需要补充什么?

非常感谢您的帮助,谢谢。

2 个回复
最合适的回答,由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
    )
)

结束

相关推荐

Modifying a Loop to Show More

此循环显示主题选项中指定的特定类别中超过4个帖子标题的一篇特色帖子。我想做的是显示4个标题中的所有4个帖子,在4个标题之上。这4个标题来自这段代码<a class=\"listtitle\" href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"<?php printf( esc_attr__( \'Permalink to %s\', \'wpnewspaper\' ), the_title_attribute( \