原文来自:https://www.zixuephp.com
ecshop的文章功能实在是太过于简单了,所以我们为了丰富网站的内容,一般会集成DEDECMS来添加一些资讯文章,主要是DEDECMS文章处理相当强大,便于二次开发,下面通过两大步骤实现ecshop在首页调用dedecms文章:
一、在index.php中的添加和修改的两步:
1、在index.php里载入DEDECMS数据库文件,这样才能查询数据库:
define('IN_ECS', true);
/*载入dede*/
require_once(dirname(__FILE__)."/dede/include/common.inc.php");
2、把查询到的文章列表赋给dede_articles数组:
$smarty->assign('dede_articles', get_dede_articles()); //dede文章列表
3、获取dedecms的文章
/**
*
* 获取dedecms的文章
* @authorhttp://www.majiaping.com/
* @return 文章列表
*/
function get_dede_articles() {
//文档排序的方式
$orderby = 'rand';
$ordersql = '';
if($orderby=='hot' || $orderby=='click') $ordersql = " ORDER BY arc.click $orderWay";
else if($orderby == 'sortrank' || $orderby=='pubdate') $ordersql = " ORDER BY arc.sortrank $orderWay";
else if($orderby == 'id') $ordersql = " ORDER BY arc.id $orderWay";
else if($orderby == 'near') $ordersql = " ORDER BY ABS(arc.id – ".$arcid.")";
else if($orderby == 'lastpost') $ordersql = " ORDER BY arc.lastpost $orderWay";
else if($orderby == 'scores') $ordersql = " ORDER BY arc.scores $orderWay";
else if($orderby == 'rand') $ordersql = " ORDER BY rand()";
else $ordersql = " ORDER BY arc.sortrank $orderWay";
//limit条件
$line = 10;
$limit = trim(preg_replace('#limit#is', '', $limit));
if($limit!='') $limitsql = " LIMIT $limit ";
else $limitsql = " LIMIT 0,$line ";
$orwhere = '';
if(isset($orwheres[0])) {
$orwhere = join(' And ',$orwheres);
$orwhere = preg_replace("#^ And#is", '', $orwhere);
$orwhere = preg_replace("#And[ ]{1,}And#is", 'And ', $orwhere);
}
if($orwhere!='') $orwhere = " WHERE $orwhere ";
$addfieldsSql = '';
$addfieldsSqlJoin = '';
$sql = "SELECT
arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,tp.namerule2,tp.ispart,
tp.moresite,tp.siteurl,tp.sitepath
$addfieldsSql
FROM `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
$addfieldsSqlJoin
$orwhere $ordersql $limitsql";
$res = $GLOBALS['db']->query($sql);
$arr = array();
while ($row = $GLOBALS['db']->fetchRow($res))
{
$row['url'] = GetFileUrl($row['id'],$row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
$row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],$row['filename'],$row
['moresite'],$row['siteurl'],$row['sitepath']);
$row['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ?
sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];
$arr[] = $row;
}
return $arr;
}
二、在模板文件index.dwt中两步实现首页调用DEDECMS文章
1、单独建立一个模板文章dede_articles.lbi,内容如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!– {if $dede_articles} –>
<div class="h3Title"><h3>相关文章</h3></div>
<ul class="brandList">
<!– {foreach from=$dede_articles item=article} –>
<li><a href="http://blog.163.com/bgh1988@126/blog/{$article.url}" title="{$article.title}" rel="external">{$article.short_title|escape:html}
</a>
</li>
<!– {/foreach} –>
</ul>
<!– {/if} –>
2、在index.dwt中需要显示文章的地方包含dede_articles.lbi文件,实现ecshop首页调用dedecms文章:
<!– #BeginLibraryItem "/library/dede_articles.lbi" –><!– #EndLibraryItem –>