I want to show 5 posts in the sidebar, EXCLUDING all those post I tagged “hp” (ID=11). Apparentely there are a lot of way to do that with categories, but with tags it is really hard to find.
Here is the code I have.
<?php query_posts( array( 'tag' => -11, 'posts_per_page' => 5, 'orderby' => 'date', 'order' => 'DESC' ) ); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if (class_exists('MultiPostThumbnails')
&& MultiPostThumbnails::has_post_thumbnail('post', '3-image')) :
MultiPostThumbnails::the_post_thumbnail('post', '3-image'); endif; ?>
<div>in <?php the_category(', '); ?></div>
<?php the_title(); ?>
<?php
$count = 0;
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$count++;
if ($count <= 1 ) {
echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a> ';
}
}
}
?>
<?php endwhile;?>
It shows only 5 posts, showing only the first tag, but it keeps the posts with the tag I am trying to exclude.
You need to use
WP_Queryclass along withtag__not_in.You can get more information about
WP_Queryclass here – http://codex.wordpress.org/Class_Reference/WP_Query