网站开发 · 2025年4月23日

优化为ecshop文章添加相似关联文章

为 ecshop 文章增加有关文章,这儿的有关其实即是类似文章了吧,假如经常看新闻网站的话,会对这个有关很了解了,比如咱们看娱乐版块的新闻,某篇娱乐新闻下面一定有其相有关的文章,这么就增加了其文章的粘性,在每一篇文章下面也有其有关文章,说白了即是类似度较高的文章了,好了说了怎样多,下面咱们来看一下详细完成的办法吧。

一,首先需要在 mysql 中运行一下 sql 语句:   INSERT INTO  `ecs_shop_config` (`id` , `parent_id` , `code` , `type` , `store_range` , `store_dir` , `value` , `sort_order`) VALUES ( ‘399’,  ‘3’,  ‘article_related’,  ‘select’,  ‘0,1’,  ”,  ‘1’,  ‘1’); ALTER TABLE  `ecs_article` ADD INDEX (  `keywords` ); 二,新建一个文件 article_related.lbi,将下面的代码保存进去,然后上传到 /themes/default/library/ 目录下.   <?php $sql_where = ” where is_open=1 and article_id != ‘” .$GLOBALS[‘article’][‘article_id’] . “‘ “; if ( $GLOBALS[‘_CFG’][‘article_related’] == ‘1’ ) { $sql_where .= ” and cat_id = ‘” . $GLOBALS[‘article’][‘cat_id’] . “‘ “; } elseif ( $GLOBALS[‘_CFG’][‘article_related’] == ‘0’ ) { //通过关键字关联 if ($GLOBALS[‘article’][‘keywords’]) {   $GLOBALS[‘article’][‘keywords’] = str_replace(“,”,  “,” , $GLOBALS[‘article’][‘keywords’]);   $key_list=explode(“,”, $GLOBALS[‘article’][‘keywords’]);   $key_id=0;   $sql_or=””;   foreach ($key_list as $keyword)   {       $sql_or .= $key_id ? ” or ” : “”;    $sql_or .= ” keywords like ‘%” . trim($keyword) . “%’ “;    $key_id++;   }   $sql_or = ” ( “. $sql_or .” ) “;   $sql_where .= ” and ” . $sql_or ; } } $sql=”select article_id, cat_id, title, open_type, file_url from ” . $GLOBALS[‘ecs’]->table(‘article’) . ” $sql_where limit 0,10 “; //echo $sql; $res_art_rel=$GLOBALS[‘db’]->query($sql); while ( $row_art_rel = $GLOBALS[‘db’]->fetchRow($res_art_rel) ) { $row_art_rel[‘url’]=$row_art_rel[‘open_type’] != 1 ? build_uri(‘article’, array(‘aid’=>$row_art_rel[‘article_id’]), $row_art_rel[‘title’]) : trim($row_art_rel[‘file_url’]); $art_rel_list[]=$row_art_rel; } $GLOBALS[‘smarty’]->assign(‘art_rel_list’, $art_rel_list); ?> <meta http-equiv=”Content-Type” content=”text/html; charset=gbk”> <style> .art_rel_tit{clear:both;width:98%;font-size:17px;font-weight:bold;padding:8px 0; margin-top:15px; text-align:left; border-bottom:1px solid #ccc;} .art_rel_box{width:98%;padding:8px 0;} .art_rel_box ul{width:100%;} .art_rel_box ul li{float:left;width:300px;height:25px;} .art_rel_box ul li a{text-decoration:none;color:#000;} .art_rel_box ul li a:hover{text-decoration:underline;color:#ff3300;} </style> <div class=”art_rel_tit”>相关文章</div> <div class=”art_rel_box”> <ul> {foreach from=$art_rel_list item=art_rel} <li>? <a href=”{$art_rel.url}” target=”_blank”>{$art_rel.title}</a></li> {/foreach} </ul> </div> <div style=”clear:both;”><br></div> 注意保存编码的格式要与整站的格式一致,否则会出现乱码的情况。 三,修改模板文件 /themes/default/article.dwt,打开后搜索如下代码:   “  <!– {if $article.content } –>{$article.content}<!– {/if} –> ” , 在这段代码的后面添加如下一段代码,位置根据自己的站点进行调整.   <!– #BeginLibraryItem “/library/article_related.lbi” –><!– #EndLibraryItem –> 四,修改 /languages/zh_cn/admin/shop_config.php 文件,在文件的最后添加上如下代码:   $_LANG[‘cfg_name’][‘article_related’] = ‘相关文章来源’; $_LANG[‘cfg_desc’][‘article_related’] = ‘设定文章详情页 “相关文章” 如何关联’; $_LANG[‘cfg_range’][‘article_related’][0] = ‘通过关键字关联’; $_LANG[‘cfg_range’][‘article_related’][1] = ‘通过文章类别关联’; OK,所有操作都已经完成了,在后台进行清理一下缓存,在前台预览一下吧。