Phpcms默认不支持随机文章调用,必须自己动手实现,以下代码只有 order=”rand()”,其它与正常调用一样。调用代码如下:
{pc:content action="lists" catid="$catid" num="8" order="rand()" return="info"}
<ul class="list lh24 f13">
{loop $info $v}
<li><a href="{$v["url"]}" target="_blank" title="{$v["title"]}"{title_style($v[style])}>{$v["title"]}</a></li>
{/loop}
</ul>
{/pc}
打开phpcms/libs/functions/extention.func.php文件,将下面的代码添加到里面即可,本修改不影响以后V9升级,因为extention.func.php文件就是官方专门用了让开发者放自己的扩展函数库。
/** * 随机读取文章列表 * * @param $modelid 模型ID 必须
* @param $catid 栏目ID 可选,为0或未指定调去全模型的数据
* @param $siteid 站点ID 可选,默认为1
* @param $thumb 是否必须有缩微图 可选,默认为0
* @param $num 调用文章数量 可选,默认为10
* @return array 返回随机文章列表数组
*/
function suiji($modelid, $catid, $siteid = 1, $thumb = 0, $num = 10) {if(!$modelid = intval($modelid))return false; $sitid = intval($siteid) ? intval($siteid) : 1;
$catid = intval($catid);
$num = intval($num);
$thumb = intval($thumb) ? " AND thumb != """ : "";
$CATEGORYS = getcache("category_content_".$siteid,"commons");
if($CATEGORYS[$catid]["child"]){
$catids_str = $CATEGORYS[$catid]["arrchildid"];
$pos = strpos($catids_str,",")+1;
$catids_str = substr($catids_str, $pos);
$sql = " AND catid IN ($catids_str)".$thumb;
} elseif($catid) {
$sql = " AND catid="$catid"".$thumb;
} else {
$sql = $thumb;
}
$c_db = pc_base::load_model("content_model");
$c_db->set_model($modelid);
$id = "";
for($i=1;$i<=$num;$i++)
{
$not = $id ? " AND id NOT IN ($id) " : "";
$rs = $c_db->query("SELECT * FROM $c_db->table_name AS t1 JOIN (SELECT ROUND(RAND()*(SELECT MAX(id) FROM $c_db->table_name WHERE status = 99 $sql)) AS id2) AS t2 WHERE t1.id>=t2.id2 and t1.status = 99 $sql $not ORDER BY t1.id ASC LIMIT 1");
while($r = mysql_fetch_array($rs,MYSQL_ASSOC)){
$return[]=$r;
$id = $id ? $id.",".$r["id"] : $r["id"];
}
}
return $return;
}
随机函数使用方法:
<div class="box">
<h5 class="title-2">随机文章</h5>
<ul class="content">
{php $data = suiji(1,0,1,0,15)}
{loop $data $r}
<li> · <a href="{$r[url]}" target="_blank" title="{$r[title]}">{str_cut($r[title], 51, "")}</a></li>
{/loop}
</ul>
</div>
如果想要调用全站随机文章,参考 phpcms全站最新文章和随机文章