所以我們?yōu)榱素S富網(wǎng)站的內(nèi)容,一般會集成DEDECMS來添加一些資訊文章,主要是DEDECMS文章處理相當(dāng)強大,便于二次開發(fā),下面通過兩大步驟實現(xiàn)
ecshop在首頁調(diào)用dedecms文章:
一、在index.php中的添加和修改的兩步:
1、在index.php里載入DEDECMS數(shù)據(jù)庫文件,這樣才能查詢數(shù)據(jù)庫:
define('IN_ECS', true);
/*載入dede*/
require_once(dirname(__FILE__)."/dede/include/common.inc.php");
2、把查詢到的文章列表賦給dede_articles數(shù)組:
$smarty->assign('dede_articles', get_dede_articles()); //dede文章列表
3、獲取dedecms的文章
/**
*
* 獲取dedecms的文章
* @author
* @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中兩步實現(xiàn)首頁調(diào)用DEDECMS文章
1、單獨建立一個模板文章dede_articles.lbi,內(nèi)容如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- {if $dede_articles} -->
<div class="h3Title"><h3>相關(guān)文章</h3></div>
<ul class="brandList">
<!-- {foreach from=$dede_articles item=article} -->
<li><a href="{$article.url}" title="{$article.title}" rel="external">{$article.short_title|escape:html}
</a>
</li>
<!-- {/foreach} -->
</ul>
<!-- {/if} -->
2、在index.dwt中需要顯示文章的地方包含dede_articles.lbi文件,實現(xiàn)ecshop首頁調(diào)用dedecms文章:
<!-- #BeginLibraryItem "/library/dede_articles.lbi" --><!-- #EndLibraryItem -->