次のスニペットは、「最近の投稿」をリストするサイドバーウィジェットからのものです。それがホームページにあり、私がそのページに目立つように私の最新のスティッキーポストを特集しているので、私はこのループでスティッキーをスキップしたいと思います。ただし、post_not_in=sticky_posts
は効果がありません。
<?php
$the_query = new WP_Query("showposts=$number&offset=1&order=ASC&post_not_in=sticky_posts");
while ($the_query->have_posts()) : $the_query->the_post();
$do_not_duplicate = $post->ID; ?>
私は@ tnorthcuttの回答を query_posts()
のスティッキパラメータについてのWordPressのコーデックス を使って作成した例えば、あなたはあなたのウェブサイトのルートにtest.php
としてドロップし、あなたのドメインを置き換えて、このようなURLにナビゲートすることによってそれが実行されるのを見ることができます:
コードに関する注意事項 クエリ文字列と同等の配列 post__no_in
引数をカンマで区切った文字列として渡すことができないため、WP_Query()
に渡しました。
また、offset=1
の代わりにoffset=0
で始めることは、そうでなければクエリによって返される最初の投稿を除外することを意味します。もちろん、適用可能な投稿が1つ多いと仮定すると、$number
で指定された投稿の数を得ることができます。だからここにコードがあります:
<?php
header('Content-Type:text/plain');
include "wp-load.php";
$number = 5;
$the_query = new WP_Query(array(
'showposts' => $number,
'offset' => 1, // This will cause the query to skip over first post
'order' => 'ASC',
'post__not_in' => get_option("sticky_posts"),
));
while ($the_query->have_posts()) : $the_query->the_post();
the_title();
endwhile;
クエリからすべてのスティッキーポストを除外したい場合は、
query_posts(array("post__not_in" =>get_option("sticky_posts")));
(from コーデックス から)
ただし、それは3.0以降でのみ動作します。 http://wordpress.org/support/topic/excluding-sticky-posts-using-query_posts
編集:下のあなたのコメントに応えて、これを試してみてください(私はこれがうまくいくかどうかわからないが、うまくいけばそれはあなたが始めましょう):
<?php
$args=array(
'showposts'=>'$number',
'offset'=>'1',
'order'=>'ASC',
'post__not_in'=>get_option("sticky_posts")
);
$the_query = new WP_Query($args);
while ($the_query->have_posts()) : $the_query->the_post();
$do_not_duplicate = $post->ID; ?>