织梦(DedeCms)调用上级栏目(父类)文章的方法(附代码)

  • A+
所属分类:PHP

DedeCms是比较知名的国产CMS,而且免费开源,在站长界有着很多的拥护者,尤其它的简单易用更是给一些想建站的朋友降低了门槛。织梦的模板框架是他们自己开发,总体来说还是不错的,很容易上手。今天团队的一个站需要在内容页调用父类下的所有内容(就是当前栏目平行栏目),大概翻了下,DEDE默认是没有这个功能的,只好自己动手了,当时因为手头有其他工作所以就直接在模板文件中实现了,晚上回来整理了下,把其他的方法也列出来。

模板标签({dede:type})中嵌套php代码

直接修改要调用的模板文件,在调用位置填写以下代码(请根据自身情况调整html标记)

{dede:type}
[field:id runphp="yes"]
global $cfg_Cs, $dsql;
$tids = GetSonIds($cfg_Cs[@me][0]);
if ('' != $tids) {
$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 FROM `#@__archives` AS arc LEFT JOIN `#@__arctype` tp ON arc.typeid=tp.id WHERE arc.arcrank>=0 AND arc.typeid IN (' . $tids . ') ORDER BY RAND() LIMIT 0,12' ;
$dsql->Execute('me', $sql);
$s = '';
while ($row = $dsql->GetArray('me')) {
$s .= '

  • ' . MyDate('m-d',$row['pubdate']) . ' ' . ('' == $row['shorttitle'] ? $row['title'] : $row['shorttitle']) . '
  • ';
    }
    @me = $s;
    } else {
    @me = '';
    }
    [/field:id]
    {/dede:type}

    修改默认arclist标签让其支持此功能

    该标签的文件在“/include/taglib/arclist.lib.php”,用任意文本编辑将其打开,查找“//指定了getall属性或主页模板例外”这句注释内容,在其下面插入代码,并把紧邻的“if”关键字改成“elseif”,代码如下:

    //指定了多个栏目时,不再获取子类的id
    if ('parent' == $typeid)
    {
    $typeid = GetSonIds($envs['reid'], 0, 0);
    $orwheres[] = ' arc.typeid in (' . ('' == $typeid ? '0' : $typeid) . ') ';
    }
    elseif( ereg(',', $typeid) )
    {
    //指定了getall属性或主页模板例外
    if($getall==1 || empty($refObj->Fields['typeid']))
    {
    $typeids = explode(',', $typeid);
    foreach($typeids as $ttid) {
    $typeidss[] = GetSonIds($ttid);
    }
    $typeidStr = join(',', $typeidss);
    $typeidss = explode(',', $typeidStr);
    $typeidssok = array_unique($typeidss);
    $typeid = join(',', $typeidssok);
    }
    $orwheres[] = " arc.typeid in ($typeid) ";
    }

    添加自定义模板标签“{dede:parentlist}”

    织梦的模板引擎支持用户自定义标签来实现它没有的功能,而且无需修改多余的代码,只要将自己的标签文件放入“/include/taglib/”下就行了,下面是我临时写的一个标签,基本实现了前文所说的功能。

    [field:pubdate function="MyDate('m-d', @me)"/] [field:title/]

    * {/dede:parentlist}
    */
    if (!defined('DEDEINC'))
    die('Request Error!');

    function lib_parentlist(&$ctag, &$refObj) {
    global $dsql, $envs;

    //属性处理
    $attlist="row|12,titlelen|40,infolen|200,orderby|id,orderby|desc,self|0";
    FillAttsDefault($ctag->CAttribute->Items, $attlist);
    extract($ctag->CAttribute->Items, EXTR_SKIP);
    $revalue = '';

    $innertext = $ctag->GetInnerText();
    $ctp = new DedeTagParse();
    $ctp->SetNameSpace('field', '[', ']');

    //对一些数字型的参数进行验证
    if (!is_numeric($row))
    $row = 12;
    if (!is_numeric($titlelen))
    $titlelen = 40;
    if (!is_numeric($infolen))
    $infolen = 200;
    $self = '0' != $self && '1' != $self ? 0 : (int) $self;

    //处理排序方式
    if (!in_array($orderby, array('id', 'pubdate', 'rand', 'click', 'scores', 'lastpost')))
    $orderby = '';
    if ('rand' == $orderby) {
    $orderby = ' ORDER BY RAND()';
    } elseif ('' != $orderby) {
    $orderway = strtoupper($orderway);
    $orderby = ' ORDER BY ' . $orderby . ' ' . ('DESC' != $orderway && 'ASC' != $orderway ? 'DESC' : $orderway);
    }

    //获取父类下的所有子类
    $tids = GetSonIds($envs['reid'], 0, $self);
    if ('' == $tids)
    return '';

    $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 FROM `#@__archives` AS arc LEFT JOIN `#@__arctype` tp ON arc.typeid=tp.id WHERE arc.arcrank>-1 AND arc.typeid IN ({$tids}){$orderby} LIMIT 0,{$row}";
    $dsql->Execute('me' ,$sql);
    $restr = '';
    while($row = $dsql->GetArray('me')) {
    //处理标题信息
    $row['fulltitle'] = $row['title'];
    if ('' == $row['shorttitle'])
    $row['shorttitle'] = $row['title'];
    $row['title'] = cn_substr($row['title'], $titlelen);
    if ('' != $row['color'])
    $row['title'] = '' . $row['title'] . '';
    if (strpos($row['flag'], 'b'))
    $row['title'] = '' . $row['title'] . '';

    //处理摘要
    $row['info'] = cn_substr($row['description'], $infolen);

    //处理缩略图
    $row['image'] = '' . preg_replace('[<]', '', $row['title']) . '">';
    if ('-' == $row['litpic'] || '' == $row['litpic'])
    $row['litpic'] = $GLOBALS['cfg_cmspath'] . '/images/defaultpic.gif';
    if ('Y' == $GLOBALS['cfg_multi_site'] && !preg_match('/^http:\/\//', $row['litpic']))
    $row['litpic'] = $GLOBALS['cfg_mainsite'] . $row['litpic'];
    $row['picname'] = $row['litpic'];

    //处理链接
    $row['arcurl'] = 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['imglink'] = '' . $row['image'] . '';
    $row['typeurl'] = GetTypeUrl($row['typeid'], $row['typedir'], $row['isdefault'], $row['defaultname'], $row['ispart'], $row['namerule2'], $row['moresite'], $row['siteurl'], $row['sitepath']);
    $row['typelink'] = '' . $row['typename'] . '';
    $row['textlink'] = '' . $row['title'] . '';

    //替换模板
    $ctp->LoadSource($innertext);
    foreach ($ctp->CTags as $tagid => $ctag) {
    if (!empty($row[strtolower($ctag->GetName())]))
    $ctp->Assign($tagid, $row[$ctag->GetName()]);
    }
    $restr .= $ctp->GetResult();
    }
    return $restr;
    }
    ?>

    然后就可以像“arclist”一样使用了,比如:

    {dede:parentlist row='10' titlelen='34'}

  • [field:title/]
  • {/dede:parentlist}

    Via:https://www.xingdonghai.cn/dedecms-get-parent-type-content/

    avatar

    发表评论

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    目前评论:3   其中:访客  3   博主  0

      • avatar 饲料粉碎机 1

        这代码有些看不懂了

        • avatar 无暃 0

          好,学习了

          • avatar 厅要 0

            留言是种美德,写点什么…灯鹭插件太可恶了,在每个网页都加入谷歌统计的代码,整个网站的数据都被它们看得一清二楚,做个插件也不要这么霸道吧,在评论框下面查看网页代码才发现多这么多代码,还有更多的隐藏的script那就更可怕了:

            var _gaq = _gaq || [];

            _gaq.push([‘_setAccount’, ‘UA-23730442-4’]);

            _gaq.push([‘_addOrganic’,’baidu’,’word’]);

            _gaq.push([‘_addOrganic’,’so’,’w’]);

            _gaq.push([‘_addOrganic’,’3721′,’name’]);

            _gaq.push([‘_addOrganic’,’yodao’,’q’]);

            _gaq.push([‘_addOrganic’,’vnet’,’kw’]);

            _gaq.push([‘_addOrganic’,’sogou’,’query’]);

            _gaq.push([‘_setVar’,’50869denhZu41v9PvCue0lR67oEB0A’]);

            _gaq.push([‘_trackPageview’]);

            (function() {

            var ga = document.createElement(‘script’);

            ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;

            ga.setAttribute(‘async’, ‘true’);

            document.documentElement.firstChild.appendChild(ga);

            })();