首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

帝國 CMS 靈動標簽實現頭條、置頂、推薦圖標顯示完全教程

     編輯:PHP 相關知識

前言

帝國 CMS(EmpireCMS)作為國內老牌企業級 CMS,其模板系統支援「靈動標簽」在列表頁、首頁靈活輸出內容。常見需求:根據文章屬性(有圖、置頂、推薦、頭條)在標題前顯示對應圖標。本文系統整理靈動標簽與列表模板兩種場景的實現代碼,並提供組合判斷邏輯,可直接複製應用。

核心字段對照表

字段含義值說明
titlepic標題圖片非空表示有縮略圖
isgood推薦1=推薦,0=不推薦
firsttitle頭條1=頭條,0=非頭條
istop置頂1=置頂,0=不置頂
truetime真實發布時間Unix 時間戳

場景一:首頁/封面模板(靈動標簽 [e:loop])

變量說明

  • $bqr:字段值陣列,如 $bqr['title']、$bqr['isgood']
  • $bqsr:系統回傳值,如 $bqsr['titleurl'](標題鏈接)

範例:3 天內新文章顯示 NEW 圖標 + 推薦/置頂/頭條組合圖標

[e:loop={'selfinfo',20,0,0}]
<?php
$newimg = '';
$good   = '';

// 有標題圖顯示圖片圖標
if (!empty($bqr['titlepic'])) {
    $newimg = "<img src='https://www.example.com/uploads/allimg/140411/22561T200-0.gif' alt='圖文'>";
}

// 置頂優先級最高
if ($bqr['istop'] == 1) {
    $good = "<img src='https://www.example.com/uploads/allimg/140411/22561UD6-1.gif' alt='置頂'>";
} elseif ($bqr['firsttitle'] == 1) {
    $good = "<img src='https://www.example.com/uploads/allimg/140411/22561UD6-2.gif' alt='頭條'>";
} elseif ($bqr['isgood'] == 1) {
    $good = "<img src='https://www.example.com/uploads/allimg/140411/22561UD6-3.gif' alt='推薦'>";
}

// 3 天內顯示 NEW
if (time() - $bqr['truetime'] <= 3 * 24 * 3600) {
    $newimg .= "<img src='https://www.example.com/uploads/allimg/140411/22561T200-1.gif' alt='NEW'>";
}
?>
<tr class="even">
    <td class="title">
        <h1>
            <a href="<?=$bqsr['titleurl']?>" target="_blank"><?=esub($bqr['title'], 30, false)?></a>
             <?=$newimg?> <?=$good?>
        </h1>
        <p class="intro"><?=esub($bqr['smalltext'], 80, false)?>[<a href="<?=$bqsr['titleurl']?>" target="_blank">詳細內容</a>]</p>
    </td>
    <td><?=$bqr['myarea']?></td>
    <td><?=date('Y-m-d H:i:s', $bqr['newstime'])?></td>
</tr>
[/e:loop]

場景二:列表頁模板(list.var,勾選「使用程式碼」)

變量說明

  • $r:字段值陣列,如 $r['title']、$r['isgood']
  • 最終模板內容需賦值給 $listtemp

範例:推薦/置頂/頭條圖標輸出

<?php
$newimg = '';
if ($r['isgood'] == 1) {
    $newimg = "<img src='https://www.example.com/icon/good.gif' alt='推薦'>";
} elseif ($r['firsttitle'] == 1) {
    $newimg = "<img src='https://www.example.com/icon/first.gif' alt='頭條'>";
} elseif ($r['istop'] == 1) {
    $newimg = "<img src='https://www.example.com/icon/top.gif' alt='置頂'>";
}

$listtemp = '<li><a href="[!--titleurl--]" title="[!--oldtitle--]">[!--title--]</a>' . $newimg . '</li>';
?>

常用組合判斷速查

<?php
// 單一條件
if ($bqr['isgood'] == 1)       // 推薦
if ($bqr['firsttitle'] == 1)   // 頭條
if ($bqr['istop'] == 1)        // 置頂

// 組合條件(同時滿足)
if ($bqr['isgood'] && $bqr['firsttitle'] && $bqr['istop']) // 置頂+頭條+推薦

// 優先級判斷:置頂 > 頭條 > 推薦
$badge = '';
if ($bqr['istop'])       $badge = 'top';
elseif ($bqr['firsttitle']) $badge = 'first';
elseif ($bqr['isgood'])  $badge = 'good';
?>

進階技巧:自定義圖標路徑與樣式類

<?php
// 定義圖標映射,便於維護
$iconMap = [
    'istop'      => ['url' => '/static/icon/top.svg',      'alt' => '置頂', 'class' => 'badge-top'],
    'firsttitle' => ['url' => '/static/icon/first.svg',    'alt' => '頭條', 'class' => 'badge-first'],
    'isgood'     => ['url' => '/static/icon/good.svg',     'alt' => '推薦', 'class' => 'badge-good'],
    'haspic'     => ['url' => '/static/icon/image.svg',    'alt' => '圖文', 'class' => 'badge-img'],
    'new'        => ['url' => '/static/icon/new.svg',      'alt' => '最新', 'class' => 'badge-new'],
];

$badges = '';
foreach (['istop', 'firsttitle', 'isgood'] as $key) {
    if (!empty($bqr[$key])) {
        $ic = $iconMap[$key];
        $badges .= "<img src='{$ic['url']}' alt='{$ic['alt']}' class='{$ic['class']}'>";
        break; // 只顯示最高優先級
    }
}
if (!empty($bqr['titlepic'])) {
    $ic = $iconMap['haspic'];
    $badges .= "<img src='{$ic['url']}' alt='{$ic['alt']}' class='{$ic['class']}'>";
}
if (time() - $bqr['truetime'] <= 3 * 86400) {
    $ic = $iconMap['new'];
    $badges .= "<img src='{$ic['url']}' alt='{$ic['alt']}' class='{$ic['class']}'>";
}
?>
<h2><a href="<?=$bqsr['titleurl']?>"><?=esub($bqr['title'], 36)?></a> <?=$badges?></h2>

常見問題排查

  • 圖標不顯示:檢查圖片路徑是否為絕對路徑、檔案是否存在、權限是否可讀
  • 字段值為空:確認後台「管理字段」中對應字段已啟用、資料表確有資料
  • 時間判斷失效:truetime 與 newstime 可能不同,前者為真實發布時間,後者可能為排序時間
  • 列表模板變量錯誤:list.var 必須用 $r,不能用 $bqr;且必須賦值給 $listtemp

結語

帝國 CMS 靈動標簽提供了極大的模板靈活性,掌握 $bqr/$r 字段陣列與條件判斷邏輯,即可實現各種「標簽圖標」業務需求。建議將圖標路徑、優先級邏輯封裝為公共片段(或自定義函數),統一維護,避免模板散落重複代碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved