POST TIME:2018-12-07 13:17
找了好多文章和教程,卻發現dedecms的標簽底層模板字段不包括這個字段呢?這就大大限制了靈活性,但dede也不可能讓所有字段都允許調用的,那樣就會大大降低系統效率,所以今天分享的是一個比較完美解決這個問題的方法,配合dede標簽,幾乎可以說沒有什么不能調用的了
首先把上面代碼放到 include/extend.func.php 里
使用方法:
function table($table, $field, $id)
{
global $dsql;
$primarys = array();
$table = strpos($table, ‘#@_’) === false?(strpos($table, ‘dede_’) === false?’dede_’.$table:str_replace(‘dede_’,’dede_’,$table)):$table;
$dsql -> Execute(“me”,”SHOW COLUMNS FROM `$table`”);
while ($r = $dsql->GetArray())
{
if($r['Key'] == ‘PRI’) $primarys[] = $r['Field'];
}
if(!empty($primarys))
{
$primary = $primarys[0];
$result = $dsql -> GetOne(“SELECT `$field` FROM `$table` WHERE `$primary`= $id”);
}
return isset($result[$field])?$result[$field]:”;
}
利用自定義函數對標簽進行擴展
如:
{dede:標記 function=’table(“要調用的表名”,”要調用的字段”,@me)’/}
這里的“標記”就是要調用的表的主鍵的值,常見的就是id和aid、mid、uid之類的
標簽底層模板內
[field:字段 function=table(“要調用的表名","要調用的字段",@me)/]
舉例
例如arclist標簽底層模板字段是沒有body字段的,就是說不能用arclist把文章內容調用出來的,當然這種需求很少,但不是沒有,現在我們就可以這樣使用
還有很多作用,如type標簽調用欄目簡介,等等
另一種DEDEcms5.7搜索結果頁面中調用自定義字段處理辦法
默認dedecms搜索頁面是沒法使用 [field:price/] 來調用 dede_addonshop 里面的 price 字段, 解決方法: 修改 include/arc.searchview.class.php 第一處: 將大約 320 行地方的代碼: if($this-ChannelType 0 || $this-ChannelTypeid 0){ if($this-ChannelType==0) $id=$this-Cha
默認dedecms搜索頁面是沒法使用[field:price/]來調用dede_addonshop里面的price字段,解決方法:
修改include/arc.searchview.class.php
第一處:
將大約320行地方的代碼:
if($this->ChannelType < 0 || $this->ChannelTypeid< 0){
if($this->ChannelType==”0″) $id=$this->ChannelTypeid;
else $id=$this->ChannelType;
$row =$this->dsql->GetOne(“Select addtable From `cn_channeltype` Where id=$id”);
$addtable = trim($row['addtable']);
$this->AddTable=$addtable;
}else{
$this->AddTable=”cn_archives”;
}
改為:
if($this->ChannelType==”0″) $id=$this->ChannelTypeid;
else $id=$this->ChannelType;
$row =$this->dsql->GetOne(“Select addtable From `cn_channeltype` Where id=$id”);
$addtable = trim($row['addtable']);
if($this->ChannelType < 0 || $this->ChannelTypeid< 0){
$this->AddTable=$addtable;
$this->AddonTable=”;
}else{
$this->AddTable=”cn_archives”;
$this->AddonTable=$addtable;
}
第二處:
將大約500行的地方的代碼:
$query = “Select arc.*,act.typedir,act.typename,act.isdefault,act.defaultname,act.namerule,
act.namerule2,act.ispart,act.moresite,act.siteurl,act.sitepath
from `{$this->AddTable}` arc left join `cn_arctype` act on arc.typeid=act.id
where {$this->AddSql} $ordersql limit $limitstart,$row”;
改為:
if (!empty($this->AddonTable)) {
$this->AddonTable=”left join `{$this->AddonTable}` addon on addon.typeid=arc.typeid”;
}else {
$this->AddonTable=”;
}
$query = “Select arc.*,act.typedir,act.typename,act.isdefault,act.defaultname,act.namerule,
act.namerule2,act.ispart,act.moresite,act.siteurl,act.sitepath,addon.*
from `{$this->AddTable}` arc left join `cn_arctype` act on arc.typeid=act.id {$this->AddonTable}
where {$this->AddSql} $ordersql limit $limitstart,$row”;
好了,可以在search.htm中使用[field:price/]了,只要你的模型有附加表,你就可以使用表里的任何字段
另外注意:如果附加表里有字段名和主表字段名一樣的,使用[field:xxxx/]的結果是未定義的