从一个循环中获取帖子ID,并从另一个循环排除该帖子


Take Post ID from one loop and exclude that post from the other loop

我正试图从第一个循环中已经显示的第二个循环中排除帖子。

我创建了一个页面模板来显示最新的一个名为"灵感"的自定义帖子。

我创建了一个小部件来显示来自相同帖子类型的其他帖子,并在我创建的页面模板上使用了该小部件。

这是页面模板的代码:

<?php
/**
 * Template Name: Inspiratie
 *
 * A custom page template without sidebar.
 *
 * The "Template Name:" bit above allows this to be selectable
 * from a dropdown menu on the edit page screen.
 */
get_header(); ?>
<div id="container" class="seprator">
  <div id="content" class="inspiratie-area" role="main">
    <?php $inspiratiepage_query = new WP_Query('showposts=1&post_type=inspirations');  ?>
    <?php while ($inspiratiepage_query->have_posts()) : $inspiratiepage_query->the_post(); ?>
    <div class="inspiratie-box" id="inspiratie-<?php the_ID(); ?>">
      <h2 class="entry-title orange">
        <?php the_title(); ?>
      </h2>
      <?php the_content() ?>
      <div class="clear"></div>
    </div>
    <?php endwhile; // end of the loop. ?>
  </div>
</div>
<?php get_footer(); ?>

下面是我创建的小部件的代码:

class Meer_Inspiratie extends WP_Widget {
    function Meer_Inspiratie() {
        $widget_ops = array('classname' => 'Meer_Inspiratie', 'description' => 'Display Inspiratie Posts' );
        $this->WP_Widget('Meer_Inspiratie', 'Inspiratie Post Widget', $widget_ops);
    }
    function widget($args, $instance) {
        extract($args, EXTR_SKIP);
        echo $before_widget;
        $title = $instance['title'];
        $items = $instance['items'];
        if(!empty($title))
        {
            echo $before_title;
                echo $title;
            echo $after_title;
        }
        if(!empty($items))
        { ?>
            <?php 
            $inspiratie_query = new WP_Query(   array(
                                    'post_type' => 'inspirations',
                                    'posts_per_page' =>  $items,
                                  'post__not_in'   => array(1),
                                    )
                                );  ?>
            <?php while ($inspiratie_query->have_posts()) : $inspiratie_query->the_post(); ?>
                <div class="inspiratie_query">
                    <p class="inspiratie_txt"><?php the_excerpt() ?></p>
                    <div class="inspiratie_img">
                        <?php if ( has_post_thumbnail() ) { the_post_thumbnail('inspiratie-product-thumb');}?>
                    </div>
                    <div class="inspiratie_quote">
                        <?php
                            echo '<blockquote class="inspiratie-quote">';
                            echo get_post_meta( get_the_ID(), 'my_specs_box_quote', true );
                            echo '</blockquote>';
                        ?>
                    </div>
                    <div class="clear"></div>
                    <a class="more-post" href="<?php the_permalink()?>">Lees meer</a>
                </div>
            <?php endwhile; // end of the loop. ?>
        <?php }
        echo $after_widget;
    }
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['items'] = strip_tags($new_instance['items']);
        $instance['title'] = $new_instance['title'];
        return $instance;
    }
    function form($instance) {
        $instance = wp_parse_args( (array) $instance, array( 'items' => '', 'title' => '') );
        $items = strip_tags($instance['items']);
        $title = $instance['title'];
?>      
            <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
            <p><label for="<?php echo $this->get_field_id('items'); ?>">No. of Items: <input class="widefat" id="<?php echo $this->get_field_id('items'); ?>" name="<?php echo $this->get_field_name('items'); ?>" type="text" value="<?php echo esc_attr($items); ?>" /></label></p>
<?php
    }
}
register_widget('Meer_Inspiratie');
/**
*   End Inspiratie Post Widget
**/

我正在尝试从小部件中排除我当前在内容区域中的帖子。。

提前感谢。。。。

dnt不知道你的侧边栏小部件在你的模板页面中从哪里调用,但它在你的帖子的第一个循环下面调用,你在那里拉了一个帖子

<?php 
$not_id=get_the_ID();
            $inspiratie_query = new WP_Query(   array(
                                    'post_type' => 'inspirations',
                                    'posts_per_page' =>  $items,
                                  'post__not_in'   => array($not_id),
                                    )
                                );  ?>

没有检查代码。希望这对你有用