显示知道其身份的图片

时间:2016-10-15 23:12:56

标签: wordpress wordpress-theming advanced-custom-fields

我正在尝试使用Wordpress'ACF plugin的图库字段插件。加载项是Navz Photo Gallery。它只是为您提供了获取图库照片ID的方法。现在我想实际显示图像。

通过调用以下代码

<?php if ( get_field( 'field_name') ) { ?>
    <img src="<?php the_field( 'field_name' ); ?>" />
<?php } ?>

我得到的只是实际的ID,如下:

<img src="21,22,23">

是否有人知道如何通过这组图像循环并单独显示它们,而不仅仅是ID,还有实际图像?

作为参考,ACF有一个官方(付费)图库插件,它会显示附加图像:

<?php $images = get_field('gallery'); if( $images ): ?>
<?php foreach( $images as $image ): ?>
    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endforeach; ?>
<?php endif; ?>

2 个答案:

答案 0 :(得分:0)

Navz帮我解决了这个问题:

<?php
$images = get_field('fotos_do_projeto'); if( $images ): $images = explode(',', $images); $images = array_filter($images); if( count($images)):
?>
    <ul>
    <?php foreach( $images as $image ): $alt = get_the_title($image); $url = get_post_meta($image, 'field_5802bd6e39e9b_url', true); ?>
        <li>
            <a href="<?php echo $url; ?>" title="<?php echo $alt; ?>">
                <?php echo wp_get_attachment_image($image, "thumbnail", false, ['alt' => $alt]); ?>
            </a>
        </li>
    <?php endforeach; endif; ?>
    </ul>
<?php endif; ?>

答案 1 :(得分:0)

你可以试试这个,

    $attachment_id = get_field( 'field_name');
    $image_attributes = wp_get_attachment_image_src( $attachment_id );
    if ( $image_attributes ) : ?>
        <img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
   <?php endif; ?>