从输出中排除当前帖子


Exclude current post from the output

我已经编写了一个Wordpress小部件,它显示来自自定义标记的自定义帖子数量(请随意使用)。这是代码:

    <?php 
    $category = get_the_category();
    $current_category = $category[0]->term_id;
    ?>
    <?php query_posts("showposts=".$posts_number."&cat=".$current_category."&tag=featured"); // the tag
    if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="block-post clearfix">
            <?php
                $thumb = '';
                $width = 287;
                $height = 162;
                $classtext = 'post-image';
                $titletext = get_the_title();
                $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Recent');
                $thumb = $thumbnail["thumb"];
            ?>
            <?php if($thumb <> '' && get_option('aggregate_thumbnails_index') == 'on') { ?>
                <div class="thumb">
                    <a href="<?php the_permalink(); ?>">
                        <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
                        <span class="overlaybig"></span>
                    </a>
                </div>  <!-- end .post-thumbnail -->
            <?php } ?>
            <div class="recenttitle"><h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
        </div> <!-- end .block-post -->
    <?php endwhile; endif; wp_reset_query(); ?>

我的问题是,如何将当前帖子从输出中排除?问题是,它不会检查用户当前是否正在查看它输出的任何帖子如何调整代码,使其跳过用户所在的当前帖子

我很确定这是一个简单的解决方案,但我现在迷失了方向。

更新:这是整个小部件的代码,包括maiorano84:提供的修复程序

<?php class ArtificePinned extends WP_Widget
{
    function ArtificePinned(){
        $widget_ops = array('description' => 'Displays posts filtered by current category and the tag pinned');
        $control_ops = array('width' => 400, 'height' => 300);
        parent::WP_Widget(false,$name='Artifice Pinned',$widget_ops,$control_ops);
    }
  /* Displays the Widget in the front-end */
    function widget($args, $instance){
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? ' ' : $instance['title']);
        $posts_number = empty($instance['posts_number']) ? '' : (int) $instance['posts_number'];
        echo $before_widget;
        if ( $title )
        echo $before_title . $title . $after_title;
?>
<?php 
$category = get_the_category();
$current_category = $category[0]->term_id;
$qarr = array(
    'posts_per_page' => $posts_number,
    'cat' => $current_category,
    'tag' => 'featured',
    'post__not_in' => get_the_ID()
);
$q = new WP_Query($qarr);
if($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
?>
    <div class="block-post clearfix">
        <?php
            $thumb = '';
            $width = 287;
            $height = 162;
            $classtext = 'post-image';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Recent');
            $thumb = $thumbnail["thumb"];
        ?>
        <?php if($thumb <> '' && get_option('aggregate_thumbnails_index') == 'on') { ?>
            <div class="thumb">
                <a href="<?php the_permalink(); ?>">
                    <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
                    <span class="overlaybig"></span>
                </a>
            </div>  <!-- end .post-thumbnail -->
        <?php } ?>
        <div class="recenttitle"><h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
    </div> <!-- end .block-post -->
<?php endwhile; endif;?>
<?php
        echo $after_widget;
    }
  /*Saves the settings. */
    function update($new_instance, $old_instance){
        $instance = $old_instance;
        $instance['title'] = stripslashes($new_instance['title']);
        $instance['posts_number'] = (int) $new_instance['posts_number'];
        return $instance;
    }
  /*Creates the form for the widget in the back-end. */
    function form($instance){
        //Defaults
        $instance = wp_parse_args( (array) $instance, array('title'=>' ', 'posts_number'=>'7') );
        $title = esc_attr($instance['title']);
        $posts_number = (int) $instance['posts_number'];
        # Title
        echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title:' . '</label><input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></p>';
        # Number Of Posts
        echo '<p><label for="' . $this->get_field_id('posts_number') . '">' . 'Number of Posts:' . '</label><input class="widefat" id="' . $this->get_field_id('posts_number') . '" name="' . $this->get_field_name('posts_number') . '" type="text" value="' . $posts_number . '" /></p>';
        # Category ?>
        <?php
    }
}// end ArtificePinned class
function ArtificePinnedInit() {
  register_widget('ArtificePinned');
}
add_action('widgets_init', 'ArtificePinnedInit');
?>
不要使用query_posts,因为它的目的是修改默认的Wordpress循环。请改用WP Query。

要回答您的问题,解决方案非常简单。我冒昧地用WP_Query:给您举了这个例子

<?php
$qarr = array(
    'posts_per_page' => $posts_number,
    'cat' => $current_category,
    'tag' => 'featured',
    'post__not_in' => get_the_ID()
);
$q = new WP_Query($qarr);
if($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
?>

请记住,我还将"showposts"更改为"posts_per_page",因为showposts在2.1版中已被弃用

更新:您的代码现在应该是这样的:

<?php 
$category = get_the_category();
$current_category = $category[0]->term_id;
$qarr = array(
    'posts_per_page' => $posts_number,
    'cat' => $current_category,
    'tag' => 'featured',
    'post__not_in' => get_the_ID()
);
$q = new WP_Query($qarr);
if($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
?>
    <div class="block-post clearfix">
        <?php
            $thumb = '';
            $width = 287;
            $height = 162;
            $classtext = 'post-image';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Recent');
            $thumb = $thumbnail["thumb"];
        ?>
        <?php if($thumb <> '' && get_option('aggregate_thumbnails_index') == 'on') { ?>
            <div class="thumb">
                <a href="<?php the_permalink(); ?>">
                    <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
                    <span class="overlaybig"></span>
                </a>
            </div>  <!-- end .post-thumbnail -->
        <?php } ?>
        <div class="recenttitle"><h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
    </div> <!-- end .block-post -->
<?php endwhile; endif;?>

修复:

问题源于此:

'post__not_in' => get_the_ID()

问题是"post__not_in"需要一个数组。不是整数。抱歉,那是我的错误。请将该行代码更改为:

'post__not_in' => array(get_the_ID())

你应该表现得很好。