2012-01-02 20 views
0

私はサイドバーに5つの投稿を表示したいと思いますが、それらの投稿を除いて「hp」というタグが付いているものはすべて(ID = 11)除外します。明らかに、カテゴリでそれを行うにはたくさんの方法がありますが、タグを使用すると見つけるのは本当に難しいです。wordpressでタグ付きの投稿を除外する方法は?

ここに私のコードです。

<?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;?> 

これは5つの投稿のみを表示し、最初のタグのみを表示しますが、除外しようとしているタグで投稿を保持します。

答えて

0

tag__not_inと一緒にWP_Queryクラスを使用する必要があります。

<?php 
$the_query = new WP_Query(array('tag__not_in' => array(11))); 

while ($the_query->have_posts()) : $the_query->the_post(); 

    // Whatever you want to do goes here 

endwhile; 

// Reset Post Data 
wp_reset_postdata(); 
?> 

あなたはここにWP_Queryクラスの詳細情報を取得することができます - それは完璧に動作http://codex.wordpress.org/Class_Reference/WP_Query

+0

を、どうもありがとうございました!しかし、どのように私は彼に5つの投稿だけを表示するように指示するのですか? –

+0

以前と同じ引数を指定できます。 – ronakg

関連する問題