ecshop调用dedecms文章列表

功能需求:在ecshop系统产品页面调用dedecms中的文章列表

系统:dedecms v5.7 + ecshop v2.7.2
实现步骤:
1.
在文章模版goods.dwt添加一个新的库文件dede_articles.lbi
themes/default/goods.dwt

 <!-- #BeginLibraryItem "/library/dede_articles.lbi" --><!-- #EndLibraryItem -->

2.
themes/default/library/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="{$article.url}"  title="{$article.title}" rel="external">{$article.short_title|escape:html}

</a>
        </li>
  <!-- {/foreach} -->
</ul>
<!-- {/if} -->

3.
goods.php载入dede,请根据你的目录结构修改

define('IN_ECS', true);
/*载入dede*/
require_once(dirname(__FILE__)."/luxury/include/common.inc.php");

4.
goods.php调用dede数据

$smarty->assign('dede_articles', get_dede_articles()); //dede文章列表

5.get_dede_articles函数简单实现

/**
 *
 * 获取dedecms的文章
 * @author http://www.heui.org
 * @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;

}

总结:最主要是需要实现获取文章路径, 在这个案例中我们载入dede, 直接调用GetFileUrl

Related Posts

    No related posts found

Related Search

This entry was posted in php and tagged , . Bookmark the permalink.

2 Responses to ecshop调用dedecms文章列表

  1. 咖啡机网 says:

    从第三步起没看懂,楼主能否给指点一下啊,没找到goods.php呢

  2. maweb says:

    提示,是怎么回事
    Fatal error: Call to undefined method DedeSql::fetchRow() in

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>