引用外部文本手动更新小工具微博

标题应该写的够清楚吧,之前就很想在侧边栏引用微博内容,无奈自己没有能力解决,想来想去想到了一个自认为比较方便的方法,引用外部的TXT文本内容,虽然也是比较麻烦的,但我这种手机流量用户可以省去不少流量,下载一个FTP文件管理软件,直接编辑根目录的TXT文件即可,比去加载整个WordPress后台省流量多了,废话真是不少!

默认的小工具侧栏是不支持PHP代码的,所以先要解决这个问题,贴入如下代码:

//小工具中运行PHP代码
add_filter('widget_text', 'php_text', 99);
function php_text($text) {
if (strpos($text, '<' . '?') !== false) {
ob_start();
eval('?' . '>' . $text);
$text = ob_get_contents();
ob_end_clean();
}
return $text;
}

现在小工具已经支持PHP,然后贴如如下代码引用文本:

<?php
$myfile = fopen("weibo.txt", "r");
while(!feof($myfile)) {
 echo "<a target='_blank' rel='nofollow' href='http://weibo.com/Guihantongxue'>归寒同学</a>:" .fgets($myfile) ;
}
fclose($myfile);
?>

11.26更换代码,显示最新5条文本信息:

<?php
$weibo = file('weibo.txt');
echo "<a target='_blank' rel='nofollow' href='https://weibo.com/Guihantongxue'>归寒同学</a>:" .$weibo[1];
echo "<a target='_blank' rel='nofollow' href='https://weibo.com/Guihantongxue'>归寒同学</a>:" .$weibo[2];
echo "<a target='_blank' rel='nofollow' href='https://weibo.com/Guihantongxue'>归寒同学</a>:" .$weibo[3];
echo "<a target='_blank' rel='nofollow' href='https://weibo.com/Guihantongxue'>归寒同学</a>:" .$weibo[4];
echo "<a target='_blank' rel='nofollow' href='https://weibo.com/Guihantongxue'>归寒同学</a>:" .$weibo[5];
?>

随机显示weibo.txt文本内5条文本

<?php
$file = "weibo.txt";
$num = 5;
$file_arr = file($file);
$rand_keys = array_rand($file_arr, $num); 
foreach ($rand_keys as $rand_key) {
echo $file_arr[$rand_key];
}
?>

然后在根目录创建weibo.txt,一行一条,通过编辑文本就可以更新微博内容了,其实我觉得这东西又麻烦有没有什么卵用,记录一下吧。

公益传播因为有你,爱不罕见。

转载请注明出处:大刘子 » 引用外部文本手动更新小工具微博

返回首页