使用Anchor CMS隐藏图像的空自定义字段

时间:2014-07-10 01:27:14

标签: php custom-fields anchor-cms

此行的解释是Anchor不会正确显示自定义字段中的图像。所以我不得不添加路径的缺失部分。它工作正常,但现在的问题是,当在现场图像中没有获取图像时,我在Safari上获得了可怕的图标:

<?php echo "<img src='http://www.firstpartoftheurl/" . article_custom_field('image') . "' alt=' ' height='300' >"; ?>

我是否只在填充自定义字段时显示此行? 如何在空白时隐藏自定义字段?

2 个答案:

答案 0 :(得分:0)

这就是我解决它的方法:

<?php
$image = article_custom_field('image');
if (!empty($image)) { 
echo "<img src= 'http://www.firstpartoftheurl".article_custom_field('image')."' alt='blabla ".article_title()."'>"; //if there is image show it
} else {
//if not do nothing
} ?>

我希望它有所帮助。它对我有用,但如果有人有更好的解决方案,请告诉我们。

答案 1 :(得分:0)

更短的时间:

<?php if (article_custom_field('featured-img')) :?>
      <img src="<?php echo article_custom_field('featured-img')?>" alt="<?php echo article_title(); ?>" />
<?php endif; ?>    

如果article_custom_field()没有返回完整的网址,那么您的服务器配置可能会出现问题,因为这一直对我有用。否则,只需添加$_SERVER['SERVER_NAME'] ...这比对网址进行硬编码要好。

此处有关于此功能的文档,其中还说明了如何使用其后备:http://anchorcms.com/docs/function-reference/articles