我编写了一个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');
?>
答案 0 :(得分:1)
不要使用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',因为版本2.1中不再使用showposts
更新:您的代码现在应如下所示:
<?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;?>
<强> FIX:强>
问题源于此:
'post__not_in' => get_the_ID()
问题是'post__not_in'需要一个数组。不是整数。道歉,那是我的错。请将该行代码更改为:
'post__not_in' => array(get_the_ID())
你应该好。