gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:11:11 25 4
gpt4 key购买 nike

我在我的 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 - acf,从字段获取缩略图大小的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21526816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com