订单随机,页面上没有重复

时间:2013-09-30 10:45:52

标签: wordpress

可以随机循环而不重复输入吗?

这是我的代码,但在第2页中,重复第1页的帖子。

<?php global $query_string;

query_posts( $query_string . '&orderby=rand' ); ?>

<?php while(have_posts()): the_post(); ?>

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <div class="post-content">

1 个答案:

答案 0 :(得分:2)

来自https://wordpress.stackexchange.com/questions/31647/is-it-possible-to-paginate-posts-correctly-that-are-random-ordered

的解决方案

的functions.php

session_start();

add_filter('posts_orderby', 'edit_posts_orderby');

function edit_posts_orderby($orderby_statement) {

    $seed = $_SESSION['seed'];
    if (empty($seed)) {
      $seed = rand();
      $_SESSION['seed'] = $seed;
    }

    $orderby_statement = 'RAND('.$seed.')';
    return $orderby_statement;
}
相关问题