经过多次尝试和错误后,WP瞬态救了我!!
这是为可能需要此功能的任何人提供的解决方案。。。
在索引中。php(主循环外)我将当前的随机顺序保存在瞬态中:
$order_array = array();
while ( $the_query->have_posts() ) :
$the_query->the_post();
$order_array[] = get_the_ID() ;
endwhile;
set_transient(\'post_order\', $order_array, 100);
// the expiration can be whatever you want
// Restore original Post Data
wp_reset_postdata();
现在是单曲。php中添加了next和prev导航功能:
$next_post_index = $current_post_index+1;
echo "<br> next post index : " . $next_post_index;
$next_post_id = $array[$next_post_index];
echo " id: " . $next_post_id;
if($next_post_id != \'\') {echo "<br><a href=" . get_permalink( $next_post_id ) . ">next</a>";}
$previous_post_index = $current_post_index-1;
echo "<br> previous post index : " . $previous_post_index;
$previous_post_id = $array[$previous_post_index];
echo " id: " . $previous_post_id;
if($previous_post_id != \'\') {echo "<br><a href=" . get_permalink( $previous_post_id ) . ">previous</a>";}
这有一点额外的标记,因为我一直在重复所有内容,以检查它是否正常工作。我相信它可以被清理,但它是有效的!如果之前或之后没有其他帖子,则不会显示next或prev(即第一篇帖子只显示“next”链接)
耶,这很有效:)