隐藏与锚CMS图像的空自定义字段


Hiding empty custom field for images with Anchor CMS

这一行的解释是Anchor没有正确显示自定义字段中的图像。所以我必须添加路径缺失的部分。它工作得很好,但现在的问题是,当没有在字段image:

中获取图像时,我在Safari上得到可怕的图标。
<?php echo "<img src='http://www.firstpartoftheurl/" . article_custom_field('image') . "' alt=' ' height='300' >"; ?>

我可以只在自定义字段填充时显示这一行吗?当自定义字段为空时,我如何隐藏它们?

我是这样解决的:

<?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
} ?>

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

更短:

<?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()没有返回完整的URL,可能是您的服务器配置出了问题,因为这对我来说总是有效的。否则,只需在$_SERVER['SERVER_NAME']前加上…这比硬编码URL要好。

这里有关于该函数的文档,其中还解释了如何使用它的回退:http://anchorcms.com/docs/function-reference/articles