Wordpress: Randomize Posts & Keep Order on Reload

时间:2016-07-11 21:23:08

标签: php wordpress

I have a category with 20 posts and I would like to shuffle the posts on the page for each individual user that visits the blog.

I know I can use this to randomize the posts:

<?php query_posts('orderby=rand');?>

But how do I keep the order after its randomize so its not constantly chagning on refresh?

1 个答案:

答案 0 :(得分:0)

就像我说的那样,最简单的方法是在cookie中按顺序保存这些id,因此cookie的结果可能如下所示:

[12344,13413,13421,44445,12342]

您也可以在数据库中保存ID的顺序,并在Cookie中保存引用。更安全,但也更多的工作。我不知道或者前面的某个地方的帖子的ID是可用的,所以如果是这样的话,我建议将它们保存在你的cookie中。

在您的前端,您可以检查cookie是否已设置,如果不是随机获取帖子。如果设置了cookie,您可以这样显示:

query_posts( array( 'post__in' => $_COOKIE['cookie'], 'orderby' => 'post__in' ) );