ACF,从字段获取缩略图大小的图像

5

我在我的WordPress网站上使用高级自定义字段插件。 我正在显示来自子页面的重复器字段(“photos_presse”)的第一张图片(“photo”)。 以下是我使用的php代码。

        <?php

            $args = array(
            'post_type'      => 'page',     
            'post_parent'    => $post->ID,
            'order'          => 'ASC',
            'orderby'        => 'menu_order',
            'post_status'   => 'publish',
            'number'        => 'no limit',
            );

            $parent = new WP_Query( $args );

            if ( $parent->have_posts() ) : ?>

            <?php while ( $parent->have_posts() ) : $parent->the_post(); ?>

            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">


            <div id="parent-<?php the_ID(); ?>" class="parent-page">

            <div class="cartouche_crop">

            <?php 
                    $first_img = true; 
                        while($first_img && has_sub_field('photos_presse')): ?> 

                     <img src="<?php the_sub_field('photo'); ?>" class="artists_menu">
                    <?php $first_img = false; ?>

            <?php endwhile; ?>


            </div>

            <h1><?php the_title(); ?></h1>
            </div>          




            </a>

            <?php endwhile; ?>



            <?php endif; wp_reset_query(); ?>

这是获取第一张图片的代码部分:
<?php

    $first_img = true; 
    while($first_img && has_sub_field('photos_presse')): ?> 

    <img src="<?php the_sub_field('photo'); ?>" class="artists_menu">

    <?php $first_img = false; ?>

<?php endwhile; ?>

在重复器字段中加载图像时,它会创建缩略图,这可以在我的WordPress管理员的“媒体设置”菜单中进行设置(小,中和大)。
对于每个图像,它会创建4个文件,一个小的,一个中等的,一个大的和原始大小。
我想要做的是,不获取每个重复器字段的第一张原始图像,而是获取重复器字段的第一张中等大小的缩略图。
我找不到如何实现这一点...
有没有人能帮助我?
感谢您的帮助。
3个回答

20

您需要设置acf字段以返回图像对象(您可以在自定义字段面板中完成此操作)。

然后该字段将返回一个数组,您可以按如下方式获取所需的大小:

$image = get_sub_field('photo');

$image_url = $image['sizes']['medium'];

或者,对于缩略图,您将拥有:

$image = get_sub_field('photo');

$image_url = $image['sizes']['thumbnail'];
基本上,您将在较大图像数组的“sizes”数组中拥有WordPress创建的所有尺寸。

OP要求编辑以整合代码


OP要求编辑以整合代码

<?php

    $first_img = true; 
    while($first_img && has_sub_field('photos_presse')):

    $image = get_sub_field('photo');

    $image_url = $image['sizes']['medium'];
?> 

    <img src="<?php echo $image_url; ?>" class="artists_menu">

    <?php $first_img = false; ?>

<?php endwhile; ?>

感谢@Ovidiu Iacomi的回答...我不知道如何使用您的代码更改我的代码...您能帮助我吗?我对PHP不是很熟悉...非常感谢。 - mmdwc
非常感谢!这基本上是我在等待您的答案时所取得的成就!非常感谢您的帮助@Ovidiu lacomi。 - mmdwc
嘿,我一直在这方面遇到困难。我一直在尝试做这个确切的事情,但返回的只是全尺寸图像。我的返回值设置为图像数组(似乎已经替换了图像对象)。虽然我的代码看起来与您发布的完全相同,但程序中是否有什么变化? - Aslan French
如果图像大小小于您设置的自定义大小或小于默认的WordPress图像大小,则会获取完整的图像URL。 - Ovidiu Iacomi

1

这很容易和简单。首先,将图像返回值设置为“图像对象”,然后使用此代码

<?php $image = get_sub_field('NAMEOFYOURSUBIMAGEFIELD'); ?>
<?php if( $image ): ?>
    <img src="<?php echo $image['sizes']['YOURCUSTOMSIZE']; ?>" width="<?php echo $image['sizes']['YOURCUSTOMSIZE-width']; ?>" height="<?php echo $image['sizes']['YOURCUSTOMSIZE-height']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>

0
首先在ACF管理者中将“返回值”更改为图像对象。[需要更新选项的ACF管理者示例http://i.imgur.com/Uh1soEx.png]
在更新返回值后,您需要按照以下代码进行更新。
<?php 
$first_img = true; 
while($first_img && has_sub_field('photos_presse')):
    $image =  get_sub_field('photo');
    $image_thumb = $image['sizes']['thumbnail'] // you can add any image size available in the wordpress admin, if you want to define new image size  refer this documentation http://codex.wordpress.org/Function_Reference/add_image_size  
    echo '<img src="'.$image_thumb.'" class="artists_menu">'; 
    $first_img = false; 
endwhile; 
?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接