0

I have searched high and low to find an answer, but can't seem to find one anywhere.

<?php
$f_contents = file("x.txt"); 
$line = $f_contents[rand(0, count($f_contents) - 1)];
echo($line);
?>

Currently I am getting random results on every page load, but need to return the string only once. I am using Wordpress, so I would preferably like a way to do this only when a page is created.

Any ideas?

4

3 回答 3

2

您可以使用 PHP 内置uniqid()函数:

http://php.net/manual/en/function.uniqid.php

于 2013-07-29T14:08:07.330 回答
0

添加一个包含帖子 ID 的选项,并且仅在它尚不存在时设置此选项。

function random_option($id, $file){

if(get_option("rand_option".$id))
     return get_option("rand_option".$id);

$f_contents = file($file); 
$value = $f_contents[rand(0, count($f_contents) - 1)];
add_option( "rand_option".$id, $value );

return $value;

}

然后调用它:

echo random_option(get_the_ID(), "file.txt");
于 2013-07-29T15:32:57.527 回答
0

创建页面时,您需要向页面添加一条元信息。

您可以通过创建自己的插件来做到这一点。

于 2013-07-29T14:18:47.703 回答