姜泽玉 發表於 2024-12-18 00:00:00

详解织梦DEDECMS自己动手添加一个函数实现模板中任意调用

<p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        是否遇到过想调用某些自定义字段,却发现dedecms的标签底层模板字段不包括这个字段呢?这就大大限制了灵活性,但dede也不可能让所有字段都允许调用的,那样就会大大降低系统效率,所以今天分享的是一个比较完美解决这个问题的方法,配合dede标签,几乎可以说没有什么不能调用的了。</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        先上代码</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:js;">function&nbsp;table($table,&nbsp;$field,&nbsp;$id)
{
&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;$dsql;
&nbsp;&nbsp;&nbsp;&nbsp;$primarys&nbsp;=&nbsp;array();
&nbsp;&nbsp;&nbsp;&nbsp;$table&nbsp;=&nbsp;strpos($table,&nbsp;&#39;#@_&#39;)&nbsp;===&nbsp;false?(strpos($table,&nbsp;&#39;dede_&#39;)&nbsp;===false?&#39;&#39;.$table:str_replace(&#39;dede_&#39;,&#39;&#39;,$table)):$table;
&nbsp;&nbsp;&nbsp;&nbsp;$dsql&nbsp;-&gt;&nbsp;Execute(&quot;me&quot;,&quot;SHOW&nbsp;COLUMNS&nbsp;FROM&nbsp;`$table`&quot;);&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;($r&nbsp;=&nbsp;$dsql-&gt;GetArray())
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($r[&#39;Key&#39;]&nbsp;==&nbsp;&#39;PRI&#39;)&nbsp;$primarys[]&nbsp;=&nbsp;$r[&#39;Field&#39;];
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;if(!empty($primarys))
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$primary&nbsp;=&nbsp;$primarys;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result&nbsp;=&nbsp;$dsql&nbsp;-&gt;&nbsp;GetOne(&quot;SELECT&nbsp;`$field`&nbsp;FROM&nbsp;`$table`&nbsp;WHERE&nbsp;`$primary`=&nbsp;$id&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;isset($result[$field])?$result[$field]:&#39;&#39;;&nbsp;&nbsp;&nbsp;
}</pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        首先把上面代码放到 include/extend.func.php 里,如果没有这个文件就自己建立一个(放到 ?&gt; 前面哦)</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        然后我们就可以在模板里任意使用了。</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        使用方法:</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        利用自定义函数对标签进行扩展</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        如:</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:php;toolbar:false;">{dede:标记&nbsp;function=&#39;table(&quot;要调用的表名&quot;,&quot;要调用的字段&quot;,@me)&#39;/}</pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        这里的“标记”就是要调用的表的主键的值,常见的就是id和aid、mid、uid之类的</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        标签底层模板内</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:php;toolbar:false;"></pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><strong>举例</strong></p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        例如arclist标签底层模板字段是没有body字段的,就是说不能用arclist把文章内容调用出来的,当然这种需求很少,但不是没有,现在我们就可以这样使用&nbsp;</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        &nbsp;</p><div class="jb51code" style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;"><pre class="brush:php;toolbar:false;">{dede:arclist&nbsp;row=&#39;10&#39;&nbsp;titlelen=&#39;24&#39;}

{/dede:arclist}</pre></div><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        还有很多作用,如type标签调用栏目简介,等等,自己去挖掘吧。</p><p style="margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, &quot;Microsoft YaHei&quot;;">
        以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。</p>
頁: [1]
查看完整版本: 详解织梦DEDECMS自己动手添加一个函数实现模板中任意调用