LoginSignup
8
8

More than 5 years have passed since last update.

wordpress 記事一覧で特定の記事を除外する

Posted at

投稿詳細での関連記事など、特定の記事を表示させない一覧を作成する方法。
WP_Queryのループを用いる場合、
'post__not_in' => array(1,2,3)
を使う。array内は投稿IDになる

例:

archive.phpなど
$category = get_the_category(); 
$cateId = $category[0]->term_id;

$args = array( 
'posts_per_page'=> 3,
'cat'=> $cateId,
'post__not_in'=> array(get_the_ID())
);
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>

get_the_ID()で現在表示している投稿記事のIDを取得している

http://www.rusica.net/heft/wp-memo-6
http://www.webopixel.net/wordpress/359.html

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8