2个WordPress循环显示来自相同帖子类型的1个帖子--如何避免显示相同的帖子?

时间:2013-06-05 作者:Joshc

我的模板文件中有两个循环。两者本质上都查询同一事物。

我的标题中的此循环:

$testimonial_head = new WP_Query(array(
    \'posts_per_page\' => 1,
    \'post_type\'     => \'testimonial\',
    \'post_status\'   => \'published\',
    \'orderby\'   => \'rand\'     
));

if ($testimonial_head->have_posts()) :
我的侧栏中有一个循环:

$testimonial_sidebar = new WP_Query(array(
    \'posts_per_page\' => 1,
    \'post_type\'     => \'testimonial\',
    \'post_status\'   => \'published\',
    \'orderby\'   => \'rand\'     
));

if ($testimonial_sidebar->have_posts()) : 
当它们同时运行时,有什么方法可以停止显示与第一个标题查询相同帖子的侧栏查询?

我只是从来没有想过要展示一个复制品。

3 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

您的两个查询是相同的,这意味着您只需要其中一个查询。基本解决方案是:

global $testimonials;
$testimonials = new WP_Query(array(
    \'posts_per_page\' => 2, /* <-- get both */
    \'post_type\'     => \'testimonial\',
    \'post_status\'   => \'published\',
    \'orderby\'   => \'rand\',
    \'ignore_sticky_posts\' => true /* you need this to control post count accurately */     
));

// header
if ($testimonials->have_posts()) {
  while($testimonials->have_posts()) {
    $testimonials->the_post();
    // whatever you need to do with the post content
    break; // stop the Loop; no complex logic needed since you are printing one and moving on
  }
}

// sidebar
// pretty much the same as the above
global $testimonials;
// this Loop will pick  up where the other left off
if (!empty($testimonials) && $testimonials->have_posts()) {
  while($testimonials->have_posts()) {
    $testimonials->the_post();
    // whatever you need to do with the post content
  }
}
也许有更好的方法可以做到这一点。我会强烈考虑将整个事件合并到一个类中,并完全避免全局事件。

class Testimonial_Loop {
  static $testimonials = false;

  function get_testimonials() {
    if (empty(static::$testimonials)) {
      static::$testimonials = new WP_Query(array(
        \'posts_per_page\' => 2, /* <-- get both */
        \'post_type\'     => \'testimonial\',
        \'post_status\'   => \'published\',
        \'orderby\'   => \'rand\',
        \'ignore_sticky_posts\' => true
      ));
    }
    return static::$testimonials;
  }

  function loop() {
    $testimonials = self::get_testimonials();
    if ($testimonials->have_posts()) {
      while($testimonials->have_posts()) {
        $testimonials->the_post();
        the_title();
        // whatever you need to do with the post content
        break; // stop the Loop; no complex logic needed since you are printing on and moving on
      }
    }
  }

}

Testimonial_Loop::loop();
Testimonial_Loop::loop();
以及使用函数和静态变量的第三种解决方案:

function testimonial_loop() {
  static $testimonials = false;

  if (empty($testimonials)) {
    $testimonials = new WP_Query(array(
      \'posts_per_page\' => 2, /* <-- get both */
      \'post_type\'     => \'post\',
      \'post_status\'   => \'published\',
      \'orderby\'   => \'rand\',
      \'ignore_sticky_posts\' => true
    ));
  }

  if ($testimonials->have_posts()) {
    while($testimonials->have_posts()) {
      $testimonials->the_post();
      // whatever you need to do with the post content
      break; // stop the Loop; no complex logic needed since you are printing on and moving on
    }
  }
}
testimonial_loop();
testimonial_loop();

SO网友:soderlind

在标题循环中,将帖子id存储在。$header_post_id

在侧边栏中:

$testimonial_sidebar = new WP_Query(array(
    \'posts_per_page\' => 1,
    \'post__not_in\' => array($header_post_id), // exclude the post in header
    \'post_type\'     => \'testimonial\',
    \'post_status\'   => \'published\',
    \'orderby\'   => \'rand\'     
));

if ($testimonial_sidebar->have_posts()) : 

SO网友:Rohit Pande

“”Not a tested answer“:您可以尝试在单个模板文件中使用全局变量。此变量将用于存储ID 标题循环中显示的帖子。使用侧栏循环时使用post__not_in 您的WP_Query 这将排除标题中显示的帖子。

编辑:参考WP_Query 了解更多信息。参考this answer 用于设置全局变量。

结束

相关推荐

将变量从jQuery传递给PHP

我有一堆li的博客,但我想把它们放在一个彩盒中放大。因此,我需要知道我点击了哪个li,这样我就可以获得该ID,并在查询中使用该ID来获取该帖子的所有日期。现在我认为最好的方法是使用jQuery检索博客id,然后使用它在页脚中添加到我的查询中。 <li id=\"<?php the_ID;?>\">blog</li> <li id=\"<?php the_ID;?>\">blog</li> <