正在检查QUERY_POST中的两个类别

时间:2013-08-12 作者:SixfootJames

如何在下面的query\\u帖子中检查两个category\\u name值?

 <?php query_posts(\'category_name=recent-work&posts_per_page=1\');
   if (have_posts()) : while (have_posts()) : the_post(); ?>
所以它读起来像这样:

 <?php query_posts(\'category_name=recent-work&&category_name=plumbing&posts_per_page=1\');
   if (have_posts()) : while (have_posts()) : the_post(); ?>
非常感谢。

EDIT:

<?php
        $querySimilarWork = new WP_Query( array(
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'categories\',
                    \'field\' => \'slug\',
                    \'terms\' => array( \'recent-work\', \'plumbing\' )
                )
            ),
            \'posts_per_page\' => 4
        );
    ?>
EDIT:

如果我现在查询这个WP\\u查询,它不会返回任何内容?

<?php while ($querySimilarWork->have_posts()) : $querySimilarWork->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>   
    </li>
 <?php endwhile; ?>

1 个回复
最合适的回答,由SO网友:Eric Holmes 整理而成

不使用query_posts. 使用WP_Query, 使用它的tax_query.

$query = new WP_Query( array(
    ...
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'category\',
            \'field\' => \'slug\',
            \'terms\' => array( \'recent-work\', \'plumbing\', \'cat3\' )
        )
    ),
    \'posts_per_page\' => 1
    ...
);

结束

相关推荐

Resort get_categories

我有一个get\\u categories数组$args=array( \'orderby\' => \'id\', \'order\' => \'ASC\', \'taxonomy\' => \'wpsc-variation\', \'hierarchical\' => 1, \'hide_empty\' => 0 ); $variationCategories=get_cate