我在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(); ?>
这是获得第一个图像的代码的一部分:
<?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个文件,一个小,一个介质,一个大,一个和原始的siez。
我想做的是,我不想获得每个中继器字段的第一个原始图像,而是要获得第一个中型缩略图的中间缩略图。
我找不到该怎么做...
有人可以帮助我吗?
感谢您的帮助
您需要设置ACF字段才能返回图像对象(您可以在"自定义字段"面板中执行此操作)。
然后该字段将返回一个数组,您将能够获得想要这样的大小:
$image = get_sub_field('photo');
$image_url = $image['sizes']['medium'];
或,对于缩略图,您将拥有:
$image = get_sub_field('photo');
$image_url = $image['sizes']['thumbnail'];
基本上,您将拥有WordPress在较大图像数组的大小数组中创建的所有尺寸。
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; ?>
这很简单。首先,将图像返回值设置为"图像对象",然后使用此代码
<?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; ?>
首先将"返回值"更改为ACF Admin [ACF管理员样本中的图像对象,您需要在其中更新选项首先https://i.stack.imgur.com/rpegp。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;
?>