我编写了一个 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');
?>