高级自定义字段+图库字段 - 从库中显示第一个图像

时间:2014-02-19 23:05:43

标签: wordpress advanced-custom-fields

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

这是我到目前为止所做的:

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

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

但HTML显示了这个......

<img src="Array">

请告知。

2 个答案:

答案 0 :(得分:4)

你说你改了代码,但它有帮助吗? 如果确实如此,您应将答案标记为“已接受”,以便可以关闭该问题。

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

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

周围的'或'

答案 1 :(得分:3)

我将代码更改为此内容;

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

        <img src="<?php echo $image_1[url]; ?>" />  
相关问题