帝国CMS列表模版实现有标题图片时显示,没有时隐藏(或显示默认图片)

[ 帝国cms教程   // 2015-03-28  ]
165

在给客户做一个网站的时候,客户要求:栏目页有标题图片时显示标题图片,无标题图片时就不显示(或显示默认图片)。类似于WordPress添加特色图像的功能,帝国CMS想实现这个功能也不难,无非是一个div和php判断而已。

帝国CMS列表模版实现有标题图片时显示,没有时隐藏

前提:需要在列表模板列表内容模板(list.var) (*)初打勾使用程序代码,下面是我的一个实例

$img='<dt><a target=”_blank” href=”[!–titleurl–]”><img alt=”[!–title–]” title=”[!–title–]” width=”120″ height=”100″ src=”[!–titlepic–]”/></a></dt>
<dd>
<h2><a id=”newstitle” name=”newstitle” target=”_blank” href=”[!–titleurl–]”>[!–title–]</a></h2>
<span id=”newsresume” name=”newsresume”>[!–smalltext–]</span><a target=”_blank” href=”[!–titleurl–]” style=”color:#178AC1″>[查看原文]</a><br />
<em class=”em02″>时间:[!–newstime–]</em><em class=”em01″>来源:<span >大连银网</span></em>
</dd>
</dl>
<div class=”Bre_line”></div>';
if(empty($r[titlepic]))
{
$img='<dd>
<h2><a id=”newstitle” name=”newstitle” target=”_blank” href=”[!–titleurl–]”>[!–title–]</a></h2>
<span id=”newsresume” name=”newsresume”>[!–smalltext–]</span><a target=”_blank” href=”[!–titleurl–]” style=”color:#178AC1″>[查看原文]</a><br />
<em class=”em02″>时间:[!–newstime–]</em><em class=”em01″>来源:<span >大连淫网</span></em>
</dd>
</dl>
<div class=”Bre_line”></div>';
}
$listtemp='<li>’.$img.'<href="http://www.softhome.cc/li>';

效果如下:

帝国cms 标题图片

延伸思考:

其实也可以实现当有标题图片时显示标题图片,无标题图片时显示自己指定的图片。

$img='<img src=”[!–titlepic–]” width=”680″ height=”130″ alt=”[!–oldtitle–]” />';
if(emptyempty($r[titlepic]))
{
$img='<img src=”http://www.softhome.cc/html/images/noimg.jpg” width=”680″ height=”130″ alt=”[!–oldtitle–]” />';
}
$listtemp='<div class=”hasImg borb mb15 pb15 clearfix”>
<a href=”[!–titleurl–]” class=”preview”>’.$img.'</a>
<p class=”hasImgContent ml15 w510 fl”>
<a href=”[!–titleurl–]” class=”fb”><b>[!–title–]</b></a>
<span>[!–smalltext–]… </span>
<label>类别:<a href=”[!–this.classlink–]”>[!–this.classname–]</a> 发布时间:[!–newstime–]</label>
</p>
</div>';

跟我在讲《帝国CMS在非列表模板中灵动标签调用标题图片的替换方法》的一样,这样子虽然达到了效果,但会显得单调。我们可以利用信息编号来实现不同的标题用不同的图片。

$img='<img src=”[!–titlepic–]” width=”680″ height=”130″ alt=”[!–oldtitle–]” />';
if(emptyempty($r[titlepic]))
{
$img='<img src=”http://www.softhome.cc/html/images/[!–no.num–].jpg” width=”680″ height=”130″ alt=”[!–oldtitle–]” />';
}
$listtemp='<div class=”hasImg borb mb15 pb15 clearfix”>
<a href=”[!–titleurl–]” class=”preview”>’.$img.'</a>
<p class=”hasImgContent ml15 w510 fl”>
<a href=”[!–titleurl–]” class=”fb”><b>[!–title–]</b></a>
<span>[!–smalltext–]… </span>
<label>类别:<a href=”[!–this.classlink–]”>[!–this.classname–]</a> 发布时间:[!–newstime–]</label>
</p>
</div>';

这样一来,当第二条的信息没有标题图片时,它就自动调用2.jpg来显示,当第三条的信息没有标题图片时,它就自动调用3.jpg来显示。

帝国CMS灵动标签调用标题图片没有图片时显示默认图片:

也有部分朋友想使用帝国CMS灵动标签调用标题图片,如果没有图片时显示默认图片:

src="<?=$bqr[titlepic]?$bqr[titlepic]: '/e/data/images/notimg.gif ' ?>"

在使用会员列表函数调用会员头像时,也可以实现没有会员头像则显示默认头像。
<?=$userr[userpic]?$userr[userpic]:'/e/data/images/notimg.gif ' ?>

列表页 内容页 判断有图或者无图时调用的代码:

列表页 内容页 判断有图或者无图时调用的代码如下:

if(empty($r[titlepic]))
{
$con='无图的模板代码 列如:<li><img src="图片地址">无图模板<href="http://www.softhome.cc/li>';
}
else{
$con='有图的模板代码 列如:<div><li><img src="图片地址">有图模板<href="http://www.softhome.cc/li></div>';

}
$listtemp=$con;