web-dev-qa-db-ja.com

最近の投稿の一覧から現在の投稿を制御する方法

サイドバーのウィジェットから私の最新の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; ?>
1
Lightshadow

Post__not_in argはあなたのために働くでしょう:

$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => 7,
'post__not_in' => array( $post->ID )
);
$myposts2 = get_posts($args);
1
Andrew Hendrie