【问题标题】:Advanced Custom Fields + Gallery Field - Show First Image from the Gallery高级自定义字段 + 图库字段 - 显示图库中的第一张图片
【发布时间】:2014-02-19 23:05:43
【问题描述】:

我正在尝试仅将我的图库字段中的一张图像显示为缩略图,当查看者单击它时,会弹出一个精美的幻灯片。

这是我目前所拥有的:

<?php 
    $images = get_field('gallery'); 
    $image_1 = $images[0]; 
    ?>    

    <img src="<?php echo $image_1; ?>" />    

但是 HTML 显示了这一点……

<img src="Array">

请指教。

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    您说您更改了代码,但这有帮助吗? 如果是这样,您应该将您的答案标记为“已接受”,以便可以关闭问题。

    如果不是,那么试试这个,假设您将该字段用作图像对象(在创建图像字段时选择):

    <?php 
    $images = get_field('gallery'); // get gallery
    $image  = $images[0]; // get first image in the gallery [1] for second, [2] for third, and so on.
    
    if( $image ) : // only show the image if it exists ?> 
        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    <?php endif; ?>
    

    记住 ['value'] 周围的 ' 或 "

    【讨论】:

      【解决方案2】:

      我把代码改成了这个;

              <?php 
              $images = get_field('gallery'); 
              $image_1 = $images[0]; 
              ?>    
      
              <img src="<?php echo $image_1[url]; ?>" />  
      

      【讨论】:

      • 很确定你的意思是 " /> 你错过了 url 周围的引号
      • 你也可以使用
      • 值得一提的是,这仅在字段返回值设置为数组时有效!
      猜你喜欢
      • 2014-03-23
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多