サイドバーのウィジェットから私の最新の2つの投稿をリストします。 single.phpからこれらの投稿の1つを表示しているとき、その投稿をリストから除外し、代わりに次の投稿を順番に表示するようにします。
single.phpは次のようになります。
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
[Content]
<?php endwhile; else : ?>
[If no post is found]
<?php endif; ?>
これがPHPウィジェットのコードです。
<?php $the_query = new WP_Query( 'showposts=2' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
[Recent posts]
<?php endwhile;?>
編集 - 解決策:
<?php global $post; $args = array('showposts' => 2, 'post__not_in' => array( $post->ID )); query_posts( $args ); if (have_posts()) : while (have_posts()) : the_post(); ?>
[Recent posts]
<?php endwhile; wp_reset_query(); endif; ?>
Post__not_in argはあなたのために働くでしょう:
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => 7,
'post__not_in' => array( $post->ID )
);
$myposts2 = get_posts($args);