1、内容页模板修改
调用之前将调用信息TAG的函数,放进 e/class/userfun.php 文件 ?> 代码之前,方便在模板中调用
/*** $classid 信息栏目id $id 信息id $exp 包裹每一个tag的前后标签,用|分割,默认是<li><a href="">TAG</a></li> $limit 调用tag数量,默认10个 ***/ function dp_InfoTag($classid,$id,$exp="<li>|</li>",$limit=10){ global $dbtbpre,$empire,$public_r; $tagstr=''; $i=0; $q=explode("|",$exp); $tsql=$empire->query("select tagid from {$dbtbpre}enewstagsdata where id=".$id." and classid=".$classid.""); while($tr=$empire->fetch($tsql)){ $tt=$empire->fetch1("select * from {$dbtbpre}enewstags where tagid=".$tr['tagid']." limit 1"); if($tt['tagid']){ if($i>$limit&&$limit){ break; } $tagurl=dp_itagLink($tr['tagid']); $tagstr.=$q[0].'<a href="'.$tagurl.'" title="'.$tt['tagname'].'" class="c1" target="_blank">'.$tt['tagname'].'</a>'.$q[1]; $i+=1; } } return $tagstr; }
1、在内容页模板中使用上述dp_InfoTag函数,调用当前信息的tag
<?=dp_InfoTag($navinfor['classid'],$navinfor['id'],'|<br>',8)?>
2、列表页模板修改
$tagstr=dp_InfoTag($r['classid'],$r['id'],'| ',5); $listtemp=' <li> <h3><a href="[!--titleurl--]">[!--title--]</a></h3> <p>[!--smalltext--]</p> TAG:'.$tagstr.' </li> '; 注意需要勾选使用程序代码。
3、首页模板修改
[e:loop={"select * from {$dbtbpre}enewstags order by tagid limit 40",0,24,0}] <a href=" <?=dp_itagLink($bqr[tagid])?>" target="_blank"> <?=$bqr[ 'tagname']?> </a> [/e:loop]
4、调用最新10个TAG信息
<? $tsql=$empire->query("select * from {$dbtbpre}enewstags order by num desc limit 10"); while($tr=$empire->fetch($tsql)){ $tagurl=dp_itagLink($tr['tagid']); ?> <a href="<?=$tagurl?>" target="_blank" class="tag"> <?=$tr['tagname']?> <span class="count"></span> </a> <? } ?>
5、一级最新推荐
<? $tsql=$empire->query("select * from {$dbtbpre}enewstags where isgood=1 order by tagid desc limit 24"); while($tr=$empire->fetch($tsql)){ $tagurl=dp_itagLink($tr['tagid']); ?> <a href="<?=$tagurl?>" target="_blank"><?=$tr['tagname']?></a> <? } ?>
六,栏目ID调用:
<? $tsql=$empire->query("select * from {$dbtbpre}enewstags where cid=2 order by tagid desc limit 30"); while($tr=$empire->fetch($tsql)){ $tagurl=dp_itagLink($tr['tagid']); ?> <a href="<?=$tagurl?>" target="_blank"><?=$tr['tagname']?></a> <?php } ?>
七,关键词自动替换,放进 e/class/userfun.php 文件 ?> 代码之前
function tihuan($id) { global $empire, $dbtbpre, $public_r, $class_r; // 获取新闻的基本信息和内容 $tagr = $empire->fetch1("select id, classid from {$dbtbpre}ecms_news where id='$id' limit 1"); $f = $empire->fetch1("select newstext from {$dbtbpre}ecms_news_data_1 where id='$id' limit 1"); $newstext = $f['newstext']; // 获取新闻的标签ID $sql = $empire->query("select tagid from {$dbtbpre}enewstagsdata where id=".$tagr['id']." and classid=".$tagr['classid'].""); // 用于存储已处理的标签ID,确保不重复替换 $processedTagIds = []; while ($td = $empire->fetch($sql)) { $tr = $empire->fetch1("select * from {$dbtbpre}enewstags where tagid=".$td['tagid']." limit 1"); if ($tr['tagid'] && !in_array($tr['tagid'], $processedTagIds)) { $processedTagIds[] = $tr['tagid']; // 标记为已处理 $tagurl = dp_itagLink($td['tagid']); // 使用回调函数和正则表达式实现只替换一次 $newstext = preg_replace_callback( '/('.preg_quote($tr['tagname'], '/').')(?!([^<]*>)|([^<]*<\/a>))/i', function ($matches) use ($tagurl) { return '<a href="'.$tagurl.'" target="_blank" class="infotextkey">'.$matches[1].'</a>'; }, $newstext, 1 // 这里的1表示只替换一次 ); } } return $newstext; }
模板使用<?=tihuan($navinfor[‘id’])?> 调用