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

mmd*_*dwc 2 php wordpress jquery image advanced-custom-fields

我在wordpress网站上使用高级自定义字段插件.我正在页面上显示来自子页面的转发器字段的第一个图像("照片")('photos_presse').这是我正在使用的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(); ?>
Run Code Online (Sandbox Code Playgroud)

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

<?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; ?>
Run Code Online (Sandbox Code Playgroud)

当在转发器字段中加载图像时,它会创建缩略图图像,这可以在我的wordpress管理员的"媒体设置"菜单中设置.(小号中号大号).

它为每个图像创建4个文件,一个小,一个中,一个大和原始siez.

我想要做的不是获得每个转发器字段的第一个原始图像,我想获得转发器字段的第一个中等大小的缩略图.

我找不到怎么做......

有谁可以帮我这个?

谢谢你的帮助

Ovi*_*omi 8

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

然后该字段将返回一个数组,您将能够获得您想要的大小:

$image = get_sub_field('photo');

$image_url = $image['sizes']['medium'];
Run Code Online (Sandbox Code Playgroud)

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

$image = get_sub_field('photo');

$image_url = $image['sizes']['thumbnail'];
Run Code Online (Sandbox Code Playgroud)

基本上,你将拥有wordpress在较大图像数组的sizes数组中创建的所有大小.


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; ?>
Run Code Online (Sandbox Code Playgroud)