1.官方默认模板调用方式
{pc:content action="lists" catid="$catid" num="25" order="id DESC"} <ul> {loop $data $r} <li><span>{date('Y-m-d H:i:s',$r[inputtime])}</span><a href="{$r[url]}"{title_style($r[style])}>{$r[title]}</a></li> {/loop} </ul> <div>{$pages}</div> {/pc}
2.调用相同模型下多个栏目的文章
a.这种方法的好处:可以想取那个栏目可以自定义;缺点:要写多个id,后台增加了栏目的话得再把id写进去。
{pc:get sql="SELECT * from v9_news where status=99 and catid in(21,22,23,24,25) thumb!='' order by id DESC" num="7" return="data"} {loop $data $r} <li><a href="{$r[url]}" title="{$r[title]}" target="_blank">{str_cut($r['title'],40)}({date('m-d',$r[inputtime])})</span></a></li> {/loop} {/pc}
b.根据父栏目id,如上面的21,22,23,24,25都是 15下面的子栏目,可以这样写
{loop subcat(15) $k $v} {php $subcatid[] = $k;} {/loop} {php $subcatid = implode(',', $subcatid);} {pc:get sql="SELECT * from v9_news where status=99 and catid in ($subcatid) order by id DESC" num="7" return="data" } {loop $data $r} <li> <a href="{$CATEGORYS[$r[catid]][url]}">[{$CATEGORYS[$r[catid]][catname]}]</a> <a href="{$r[url]}" title="{$r[title]}" target="_blank">{str_cut($r['title'],40)}</a> <em>[{date('Y-m-d',$r[inputtime])}]</em> </li> {/loop} {/pc}
好处:只要写一个父栏目id,自动取下级子栏目id;缺点:循环父栏目下所有的子栏目id,如果某个子栏目的内容不想显示的话,不好实现。
3.调取某个模型下的最新文章
{pc:get sql="SELECT a.title,a.catid,a.inputtime,b.catid,b.catname,a.url as turl,b.url as curl,a.id FROM `v9_news` a,`v9_category` b WHERE a.catid=b.catid ORDER BY `a`.`id` DESC" num="9" cache="300"} {loop $data $r} 发布时间:{date('Y-m-d H:i:s',$r[inputtime])}<br> 所属分类:<a href="{$r[curl]}">{$r[catname]}</a><br> 标题链接:<a href="{$r[turl]}">{$r[title]}</a> {/loop} {/pc}
4.调用被推荐且指定栏目的文章
调用news模型中“被推荐到posid为1和2的推荐位且栏目id为6和9”的文章,start="0"表示从第一条开始调用。
{pc:get sql="SELECT * FROM v9_news WHERE id IN (SELECT id FROM v9_position_data WHERE posid in(1,2) and catid in(6,9)) order by listorder DESC" cache="3600" start="0" num="5" return="data"} {loop $data $n $r} <li><a href="{$r[url]}" target="_blank">{$r[title]}</a></li> {/loop} {/pc}
5.get方式直接用sql查询调用
用phpcms提供的get操作sql非常好用,它跳出了官方内置各种调用方式的限制,可自由灵活的按需使用。
{pc:get sql="select * from v9_news where catid=6"} {pc:get sql="select * from v9_news where catid=6 and status=99 order by updatetime desc" num="5" page="$page"} {pc:get sql="select * from v9_news where catid in(6,9) and status=99 order by updatetime desc" num="5" page="$page"}
6.修改默认的{pc:content action="lists"让它能调用多个栏目的文章(同模型)
文件位置:phpcms\modules\content\classes\content_tag.class.php,在public function lists($data)函数中
$catid = intval($data['catid']); if(!$this->set_modelid($catid)) return false; if(isset($data['where'])) { $sql = $data['where']; } else { $thumb = intval($data['thumb']) ? " AND thumb != ''" : ''; if($this->category[$catid]['child']) { $catids_str = $this->category[$catid]['arrchildid']; $pos = strpos($catids_str,',')+1; $catids_str = substr($catids_str, $pos); $sql = "status=99 AND catid IN ($catids_str)".$thumb; } else { $sql = "status=99 AND catid='$catid'".$thumb; } } //修改为: $catid = $data['catid']; if(isset($data['where'])) { $sql = $data['where']; } else { $thumb = intval($data['thumb']) ? " AND thumb != ''" : ''; if(strpos($catid,',')){ $catidd =explode(",",$catid); if(!$this->set_modelid($catidd[0])) return false; $sql = "status=99 AND catid IN ($catid)".$thumb; } elseif ($this->category[$catid]['child']){ if(!$this->set_modelid($catid)) return false; $catids_str = $this->category[$catid]['arrchildid']; $pos = strpos($catids_str,',')+1; $catids_str = substr($catids_str, $pos); $sql = "status=99 AND catid IN ($catids_str)".$thumb; } else { if(!$this->set_modelid($catid)) return false; $sql = "status=99 AND catid='$catid'".$thumb; } }
修改后的调用方式:{pc:content action="lists" catid="6,7,8" num="25" order="id DESC"}
有朋自远方来...评论一下呗O(∩_∩)O