【问题标题】:Hiding empty custom field for images with Anchor CMS使用 Anchor CMS 隐藏图像的空自定义字段
【发布时间】:2014-07-10 01:27:14
【问题描述】:

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

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

我可以仅在填充自定义字段时显示此行吗? 自定义字段为空时如何隐藏?

【问题讨论】:

    标签: php custom-fields anchor-cms


    【解决方案1】:

    我就是这样解决的:

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

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

    【讨论】:

      【解决方案2】:

      再短一点:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多