整合多个Wordpress循环

时间:2012-03-16 01:38:56

标签: php wordpress loops

我有3个循环,每个循环返回不同类型的分类法(客户端希望能够在没有编码的情况下在jQuery幻灯片中更改文本)。我可以将所有3到1的循环集成在一起查询数据库中的3种不同类型,在下面的无序列表中返回帖子,还是我坚持使用每种类型的循环?

<ul class="sub-header-excerpts">
  <li> 
     <?php 
     $args = array( 'excerpts_textboxes' => 'Excerpt One', 'posts_per_page' => 1);  
     $loop = new WP_Query( $args );  
     while ( $loop->have_posts() ) : $loop->the_post();  
     ?>      
      <span class="textbox-title"><a href="#"><?php the_title(); ?></a></span>
      <span class="textbox-excerpt"><a href="#"><?php the_excerpt(); ?></a></span>
     <?php endwhile; ?> 
  </li> 
  <li> 
     <?php 
     $args = array( 'excerpts_textboxes' => 'Excerpt Two', 'posts_per_page' => 1);  
     $loop = new WP_Query( $args );  
     while ( $loop->have_posts() ) : $loop->the_post();  
     ?>      
      <span class="textbox-title"><a href="#"><?php the_title(); ?></a></span>
      <span class="textbox-excerpt"><a href="#"><?php the_excerpt(); ?></a></span>
     <?php endwhile; ?> 
  </li>  
  <li> 
    <?php 
     $args = array( 'excerpts_textboxes' => 'Excerpt Three', 'posts_per_page' => 1);  
     $loop = new WP_Query( $args );  
     while ( $loop->have_posts() ) : $loop->the_post();  
     ?>      
      <span class="textbox-title"><a href="#"><?php the_title(); ?></a></span>
      <span class="textbox-excerpt"><a href="#"><?php the_excerpt(); ?></a></span>
     <?php endwhile;   ?> 
  </li>  
</ul>

修改

$args = array(
    'tax_query' => array(
    'relation' => 'OR',
  array(
    'taxonomy' => 'excerpts_textboxes',
    'field' => 'slug',
    'terms' => array( 'Excerpt one', 'Excerpt Two', 'Excerpt Three' )
      ),
   )
);
$loop = new WP_Query( $args ); 
while ( $loop->have_posts() ) : $loop->the_post(); 

1 个答案:

答案 0 :(得分:2)

http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
它确实提到了选择多种类别:

$args = array(
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array( 'quotes' )
        ),
        array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array( 'post-format-quote' )
        )
    )
);
$query = new WP_Query( $args );

我从来没有尝试过立即查询多个分类法,但我确实将它变成了一个婊子