while(have_posts()):the_post();一遍又一遍地重复同一个帖子?

时间:2017-01-24 01:35:15

标签: php wordpress

我对这个小片段有疑问。基本上它是在当前版本类别中抓取一个帖子并反复显示它而不是抓住并显示前3个。任何想法?我确定它是一个愚蠢的错过了“或其他什么。

    global $query_string;

    query_posts( array(
    'showposts'  => 3,
    'cat' => 'current-releases'
) );

    echo '<div class="related-posts">';

    while (have_posts()) : the_post();
       echo '<div class="related-album">'.the_post_thumbnail('large');
        echo ' '.the_title();
       echo '</div>';
    endwhile;

    echo '</div>';

3 个答案:

答案 0 :(得分:1)

wp_reset_query();之后使用endwhile。请按照功能参考,您可以在其中看到示例代码段。

https://codex.wordpress.org/Function_Reference/wp_reset_query

答案 1 :(得分:0)

您的query_posts选项需要格式化为数组:

query_posts( array(
    'showposts'  => 3,
    'cat' => 'current-releases'
) );

答案 2 :(得分:0)

不应使用

query_posts。我首先要把它换掉。请记住,通过这样做,您还需要更新循环。

$related_posts = new WP_Query( array(
    'posts_per_page'  => 3,
    'cat' => 'current-releases',
    'no_found_rows' => true,
) );

// we don't want any output if no posts found. 
if ( $related_posts->have_posts() ) : 

    echo '<div class="related-posts">';

    while ( $related_posts->have_posts() ) : $related_posts->the_post();
       echo '<div class="related-album">';

       // the_post_thumbnail() and the_title() output, not return
       the_post_thumbnail( 'large' );
       the_title( ' ' ); // I've used ' ' as the before arg since your original code did that.

       echo '</div>';
    endwhile;

endif;
相关问题