ecshop教程:文章分类或ID自动取内容第一张图做为缩略图展示

2021-09-07 0 570
  • (1)找到 网店根目录/includes/lib_article.php 文件,并在最尾处增加以下代码(大概在行 83 处):
  • function GetImageSrc($body) {
  •    if( !isset($body) ) {
  •      return ”;
  •    }
  •    else {
  •      preg_match_all (“/<(img|IMG)(.*)(src|SRC)=[“|’|]{0,}([h|/].*(jpg|JPG|gif|GIF|png|PNG))[“|’|s]{0,}/isU”,$body,$out);
  •   return $out[4];
  •    }
  • }
  • /**
  • * 按文章 ID 号或文章分类 ID 号取得文章
  • * @param  array    $id       文章 ID 或文章分类 ID
  • * @param  string   $getwhat  以何种方式取文章.当参数为’cat’时以文章分类 ID 取,其他都以文章 ID 取
  • * @param  integer  $num      控制显示多少条文章.当参数为 0 时则全部显示
  • * @param  boolean  $isrand   是否随机显示文章.
  • */
  • function get_article_new( $id = array(0), $getwhat = ”, $num = 0, $isrand = false ) {
  • $wherestr = ”;
  • $search = ”;
  • if( $getwhat ==  ‘cat’ ){
  •   $search = ‘cat_id=’;
  • }
  • else {
  •   $search = ‘article_id=’;
  • }
  • for( $i=0; $i<count($id); $i++ ) {
  •   if( $i<count($id)-1 ) {
  •    $wherestr = $wherestr . $search . $id[$i] . ‘ or ‘;
  •   }
  •   else {
  •    $wherestr = $wherestr . $search . $id[$i];
  •   }
  • }
  • $sql = ‘SELECT * FROM ‘ . $GLOBALS[‘ecs’]->table(‘article’) .
  • ‘ WHERE (‘ . $wherestr . ‘) AND (is_open = 1) ‘;
  • if ( $isrand == true ) {
  •   $sql .= ‘ ORDER BY rand()’;
  • }
  • else {
  •   $sql .= ‘ ORDER BY add_time DESC, article_type DESC, article_id DESC’;
  • }
  • if ( $num > 0 ) {
  •   $sql .= ‘ LIMIT ‘ . $num;
  • }
  • $res = $GLOBALS[‘db’]->getAll($sql);
  • $articles = array();
  • foreach ($res AS $id => $row) {
  •   $articles[$id][‘title’]   = $row[‘title’];
  •   $articles[$id][‘url’]     = ‘article.php?id=’ . $row[‘article_id’];
  •   $articles[$id][‘addtime’] = date($GLOBALS[‘_CFG’][‘date_format’], $row[‘add_time’]);
  •   $articles[$id][‘content’] = $row[‘content’];
  •   $imgsrc                   = GetImageSrc($row[‘content’]);
  •   $articles[$id][‘img’]     = $imgsrc[0];
  • }
  • return $articles;
  • }
  • (2)在模板目录的库文件目录中增加:msg_img.lbi 库文件
  • 代码如下:
  • <?php
  •     $this->assign( ‘img_art1′, get_article_new(array(2),’cat’,6) );
  • ?>
  • <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
  • <style type=”text/css”>
  • <!–
  • .img_art{ width:273px; height:130px; border:#FFF solid 1px; float:left; padding-bottom:10px; overflow:hidden; }
  • .title { background-color:#D3C08D; height:22px; color:#765935; padding:5px 5px 0px 5px; font-weight:bold;}
  • .content { padding:5px;}
  • .Limg {width:100px; height:100px; border:#E0E0E0 solid 1px; padding:1px; text-align:center; margin-right:2px; float:left;}
  • .Limg img {width:100px; height:100px; border:0px; }
  • .rcont { width:155px; float:left;}
  • .rcont li {padding-left: 2em;line-height: 180%;background-image: url(images/news_arrow.gif);background-repeat: no-repeat;background-position: 12px 5px;white-space:nowrap;width:150px;text-overflow:ellipsis;overflow:hidden;}
  • .rcont a:visited, .rcont a:link {color: #9A6F4A;text-decoration: underline;}
  • .rcont a:hover {color: #9A6F4A;text-decoration: underline;}
  • }
  • –>
  • </style>
  • <div class=”img_art”>
  • <div class=”title”>公司新闻</div>
  •     <div class=”content”>
  •     <!– {if $img_art1} –>
  •     <!–{foreach from=$img_art1 item=aimg1 name=”artimg1″}–>
  •         {if $smarty.foreach.artimg1.index eq 1 }
  •         <div class=”Limg”>
  •             <a href=”{$aimg1.url}” target=”_blank”><img src=”{$aimg1.img}” alt=”{$aimg1.title|escape:html}” /></a>
  •         </div>
  •         {/if}
  •     <!–{/foreach}–>
  •     <div class=”rcont”>
  •     <ul>
  •     <!–{foreach from=$img_art1 item=ali1 name=”artli1″}–>
  •     {if $smarty.foreach.artli1.index neq 1 }
  •      <li><a href=”{$ali1.url}” title=”{$ali1.title|escape:html}” target=”_blank”>{$ali1.title|truncate:16:”…”}</a></li>
  •     {/if}
  •     <!–{/foreach}–>
  •     </ul>
  •     </div>
  •     <!– {else} –>
  •      暂无文章
  •     <!– {/if} –>
  •     </div>
  • </div>
  • 过程一是程序的主体功能,过程二是ecshop模板显示时候的样式表现。
  • 下面讲解一下过程二里面的重要一点的代码:
  • $this->assign( ‘img_art1′, get_article_new(array(2),’cat’,6) );
  •   这是调用者 get_article_new()函数,参数文章类别 ID 号是 2,6 篇文章。当然,这个功能还支持随机根据这篇文章。但必须注意欧盟是一个缓存机制,可能会在选择随机发现文章中没有变化,但当打开浏览器,或刷新浏览器的缓存时间后显示已经改变了。
    过程 2,第 25 行代码
    {如果$ smarty。Foreach。Artimg1。情商指数 1}
    控制显示偱环图中第一个文章。如果你想显示左边的两张图片,你可以改变显示适当的条件,等等。
    过程 2,34 代码行
    {如果$ smarty。Foreach。Artli1。指数 neq 1}
    这不是重复,和文章列表显示已显示图像。
    其他基本上是风格布局。我直接写 CSS 到库文件,然后不需要改变原来的风格。CSS 文件
收藏 (0)

微信扫一扫

支付宝扫一扫

点赞 (0)

免责声明

本资源仅限个人学习与研究使用,严禁用于任何商业用途!

1 网站名称:汇站网
2 永久网址:https://www.huizhanii.com
3 本站资源来源于网友投稿和付费购买,仅供编程人员及源代码爱好者下载参考与研究,不提供任何技术支持服务!
4 资源展示图片及相关信息仅供参考,不代表本站立场!本站仅作为信息存储平台
5 禁止在服务器和虚拟机上搭建运营,所有资源仅限本地调试与研究使用,不支持联网运行!
6 未经版权方授权,严禁用于商业用途。使用者如违反国家法律法规,需自行承担全部法律责任!
7 请在下载后24小时内删除!建议支持正版授权作品
8 如资源侵犯您的合法权益,请提供版权证明及相关作品信息发送至邮箱:972908224@qq.com,我们将及时处理
9 如遇下载链接失效或支付未到账,请联系站长处理
10 欢迎投稿优质源码或教程,审核通过后将获得相应奖励
11 资源收费仅用于维持网站正常运营
12 数字商品具有特殊性质,一经购买概不退款

汇站网 ECSHOP ecshop教程:文章分类或ID自动取内容第一张图做为缩略图展示 https://www.huizhanii.com/15883.html

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

联系官方客服

为您解决烦忧 - 24小时在线 专业服务