dedecms动态设置相关文章

2013-02-02 21:25 来源:www.chinab4c.com 作者:dedecms专家

    dedecms动态设置相关文章,这个功能是十分需要的,我们可以通过对dedecms的文章增加相关文章的id字段,在录入文章和编辑文章的时候,可以修改这个字段的值,从而达到后台动态控制相关文章的文章id,然后自己编写dedecms标签realarticle来实现动态对dedecms相关文章的功能调用实现。

1:alter table dede_archives add column reaid varchar(64) not null default ''
2:后台article_edit.html article_add.html增加以下代码
<tr>
      <td height="24" class="bline">
          <table width="800" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="90">&nbsp;相关文章:</td>
            <td>
           <span id='typeid2ct'></span>
           <input type='text' name='reaid' id='reaid' value='<?php echo ($arcRow['reaid']=='0' ? '' : $arcRow['reaid']); ?>' style='width:200px;' />
           录入ID,用逗号隔开(1,2,3)
                 </td>
          </tr>
        </table>
      </td>
    </tr>

3:article_add.php article_edit.php

reaid='$reaid'修改这个字段

4:调用
{dede:realarticle limit=3}
<a href=[field:arcurl/]>[field:title/]</a><br>
{/dede:realarticle}

5:标签


<?php

function lib_realarticle(&$ctag,&$refObj)
{
    global $dsql,$envs,$cfg_cmsurl;
    $list = "limit|5,titlelen|10";
    FillAttsDefault($ctag->CAttribute->Items,$list);
    extract($ctag->CAttribute->Items);
    $text = $ctag->GetInnerText();
   

    $str = '';
    $tcp = new DedeTagParse();
    $tcp->SetNameSpace('field','[',']');
    $tcp->LoadString($text);
    if($refObj->Fields['reaid']){
        $ids = explode(",", $refObj->Fields['reaid']);
        $query = "SELECT arc.*,tp.reid,tp.typedir,ch.addtable,tp.defaultname,tp.namerule
                FROM `dede_archives` arc
                         LEFT JOIN dede_arctype tp on tp.id=arc.typeid
                          LEFT JOIN dede_channeltype as ch on arc.channel = ch.id
                          WHERE arc.id in('".join("','", $ids)."') AND arc.arcrank > -1 ";
        $dsql->SetQuery($query);
        $dsql->Query();
        while($row = $dsql->GetArray()){
                $row['filename'] = $row['arcurl'] = GetFileUrl($row['id'],$row['typeid'],$row['senddate'],$row['title'],1,
                0,$row['namerule'],$row['typedir'],0,'',$row['moresite'],$row['siteurl'],$row['sitepath']);
               
                $row['typeurl'] = GetTypeUrl($row['typeid'],$row['typedir'],$row['isdefault'],$row['defaultname'],$row['ispart'],
                $row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']);

                if($row['litpic'] == '-' || $row['litpic'] == '')
                {
                    $row['litpic'] = $GLOBALS['cfg_cmspath'].'/images/defaultpic.gif';
                }
                if(!preg_match("#^http:\/\/#i", $row['litpic']) && $GLOBALS['cfg_multi_site'] == 'Y')
                {
                    $row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic'];
                }
                $row['picname'] = $row['litpic'];
               
                $row['image'] = "<img src='".$row['picname']."' border='0' width='{$imgwidth}' height='{$imgheight}' alt='".preg_replace("#['><]#", "", $row['title'])."' />";

                $row['imglink'] = "<a href='".$row['filename']."'>".$row['image']."</a>";

                $row['stime'] = GetDateMK($row['pubdate']);
                $row['typelink'] = "<a href='".$row['typeurl']."'>".$row['typename']."</a>";
                $row['fulltitle'] = $row['title'];
                $row['title'] = cn_substr($row['title'],$titlelen);
                $row['textlink'] = "<a href='".$row['filename']."'>".$row['title']."</a>";
                $row['plusurl'] = $row['phpurl'] = $GLOBALS['cfg_phpurl'];
                $row['memberurl'] = $GLOBALS['cfg_memberurl'];
                $row['templeturl'] = $GLOBALS['cfg_templeturl'];
                foreach($tcp->CTags as $k=>$v){
                    $tcp->Assign($k,$row[$v->GetName()]);
                }
               
                $str.=$tcp->GetResult();
        }
       
        return $str;
    }else{
        return '';
    }
  
 
}

   来源:http://www.chinab4c.com