2017-09-13 161 views
0

我想在我的循环,它位于index.php中有两个不同的查询。我正在使用WP codex,但它没有工作。我希望以后每一篇文章都在自己的特殊DIV中,所以这只是我工作的开始。循环不显示帖子

问题是,代码的第二部分不起作用,我不知道为什么。据我所阅读的法典,一切都应该没问题。请帮帮我。

<div class="col1"> 
     <?php 
     $my_query = new WP_Query('category_A tym=featured&posts_per_page=1'); 
     while ($my_query->have_posts()) : $my_query->the_post(); 
      $do_not_duplicate = $post->ID; 
      ?> 
      <!-- Do stuff... --> 
      <?php get_template_part('content', get_post_format()); ?> 
     <?php endwhile; ?> 

     <!--Over here everything works fine!--> 



     <!--This code doesnt show up. It is supossed to show 1 post, only heading and date with author. But it doesnt show nothing at all.--> 

     <?php 
     $my_queryOne = new WP_Query('posts_per_page=1'); 
     while ($my_queryOne->have_posts()) : $my_queryOne->the_post(); 
      if ($post->ID == $do_not_duplicate) 
       continue; 
      ?> 
      <!-- Do stuff... --> 
      <h2 id="post-<?php the_ID(); ?>"> 
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> 
        <?php the_title(); ?></a></h2> 
      <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> 
      <?php 
     endwhile; 
     ?>  

    </div> 

回答

1

WP_Query带有一个数组作为参数 - 即

$query = new WP_Query(array('category_name' => 'featured','posts_per_page' => 1)); 

此外,运行多个查询时 - 在第一循环之后使用wp_reset_postdata()

很好的例子在这里:

https://codex.wordpress.org/Class_Reference/WP_Query

相关问题