许多企业网站在使用织梦建站的时候,经常会使用到自定义字段,由于织梦自带的字段不能满足部分企业客户需求,那么就需要在自定义模型中创建自己的字段,这样会出现一个问题,客户后台填写该字段时前台显示正常,如客户没有填写该字段,那该字段前台为空,但是还是显示了字段的位置,这里就看起来不是很舒服。
解决方法:判断字段为空时,前台隐藏不显示。
例如,我们后台添加的字段为“cjqaf”
那么我们内容页调用应该是:{dede:field.cjqaf/}
如果按照这个标签调用下来,将会出现,如果后台该字段为空白,则前台也可以调用出来。
这次我们是用附件字段进行测试,其它的字段同理
具体代码如下:
{dede:field.cjqaf runphp='yes'} if(@me<>'' )@me = '<div style="margin-top: 10px; margin-bottom: 10px; height: 50px"><span style="font-size: 16px;line-height: 50px;font-weight: bold;color: #FFFFFF;text-align: center;display: block;height: 50px;width: 200px;margin-right: auto;margin-left: auto;background-color: #2F7CD2;border-radius"><a href="'.@me.'" style="color:#FFFFFF;text-decoration: none;">点击下载</a></span></div>';else @me = ''; {/dede:field.seotag}
其中
<div style="margin-top: 10px; margin-bottom: 10px; height: 50px"><span style="font-size: 16px;line-height: 50px;font-weight: bold;color: #FFFFFF;text-align: center;display: block;height: 50px;width: 200px;margin-right: auto;margin-left: auto;background-color: #2F7CD2;border-radius"><a href="'.@me.'" style="color:#FFFFFF;text-decoration: none;">点击下载</a></span></div>
这个为正常显示的样式,大家可以这么套。
如果想实现后台字段为空,前台显示指定内容,修改方法为
{dede:field.cjqaf runphp='yes'} if(@me<>'' )@me = '<div style="margin-top: 10px; margin-bottom: 10px; height: 50px"><span style="font-size: 16px;line-height: 50px;font-weight: bold;color: #FFFFFF;text-align: center;display: block;height: 50px;width: 200px;margin-right: auto;margin-left: auto;background-color: #2F7CD2;border-radius"><a href="'.@me.'" style="color:#FFFFFF;text-decoration: none;">点击下载</a></span></div>';else @me = '前台指定显示内容'; {/dede:field.seotag}