2

現在、テンプレートに次のコードがあります。

<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><p><?php the_excerpt(); ?>
</p></li>
<?php endforeach; ?>
</ul>

コードの性質は、ブログ投稿からランダム投稿のリストを生成することです。問題は、コードが無関係なブログ投稿へのコメントの間違ったリストを表示することで、私のコメント セクションを台無しにし始めることです。

上記のリンクで私のサンプルを参照してください

やるべき常識は、私のテンプレートのコードを削除することです。私の質問は、上記のコードを修正して引き続き使用できるようにする方法についてのアイデアです。

4

1 に答える 1

2

get_posts を使用していて、$post をオーバーライドする必要がある場合は、次のようにする必要があります。

<?php
global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
    <li><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><p><?php the_excerpt(); ?>
    </p></li>
<?php endforeach; ?>
</ul>
<?php $post = $tmp_post; // reset the $post to the original ?>
于 2012-12-24T01:46:00.240 に答える