Php 获取ACF中继器字段的第一行(图像)
我正在我的网站上使用ACF插件。 我想在一个页面上显示子页面中继器字段的第一行(图像URL) 这是我网站上的页面(所有图片都已加载,但只显示第一个,但我只想加载第一个) 以下是我的页面上显示所有图像的代码:Php 获取ACF中继器字段的第一行(图像),php,jquery,wordpress,parent,advanced-custom-fields,Php,Jquery,Wordpress,Parent,Advanced Custom Fields,我正在我的网站上使用ACF插件。 我想在一个页面上显示子页面中继器字段的第一行(图像URL) 这是我网站上的页面(所有图片都已加载,但只显示第一个,但我只想加载第一个) 以下是我的页面上显示所有图像的代码: <?php $args = array( 'post_type' => 'page', 'post_parent' => $post->ID, 'order'
<?php
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$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 class="cartouche_menu_artistes">
<div id="parent-<?php the_ID(); ?>" class="parent-page">
<div class="cartouche_crop">
<?php while(has_sub_field('photos_presse')): ?> // my repeater field
<img src="<?php the_sub_field('photo'); ?>" class="artists_menu"> // my sub-field, the one I want to get the first row
<?php endwhile; ?>
</div>
<div class="bandeau"></div>
<h1><?php the_title(); ?></h1>
<div class="bandeau"></div>
</div>
</div>
<?php endwhile; ?>
</a>
<?php endif; wp_reset_query(); ?>
是照片的URL,而不是照片本身。。。这就是为什么我很难坐到第一排,但我想保持这种状态
我在网上找到了这个,但我无法使它适应我的情况:
<?php
$rows = get_field('repeater_field_name' ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['sub_field_name' ]; // get the sub field value
// Note
// $first_row_image = 123 (image ID)
$image = wp_get_attachment_image_src( $first_row_image, 'full' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />
" />
下面是一个包含我的代码的JSFIDLE:
有人能帮我吗
非常感谢您的帮助,我刚刚添加了一个简单的布尔值,在检索图像时设置为false。然后,我将布尔值添加到
while
循环的条件中,以便在完成一个图像后,while循环将失败并继续
<?php
$first_img = true;
while($first_img && has_sub_field('photos_presse')): ?> // my repeater field
<img src="<?php the_sub_field('photo'); ?>" class="artists_menu">
<?php $first_img = false; ?>
<?php endwhile; ?>
//我的中继器字段
“class=”艺术家菜单“>
希望这有帮助 @mmdwc几乎是正确的。由于中继器是一个嵌套字段,因此当您尝试检索子字段时,
get\u field
不会检索任何内容
<?php
$rows = get_sub_field('repeater_field_name' ); // get all the rows of the sub field repeater
$first_row = $rows[0]; // get the first row of the repeater
$first_row_image = $first_row['sub_field_name' ]; // get the sub field value of the nested repeater
?>
感谢您的回复@Billy Mathews,但它仍然会加载所有图像。。。你能查一下来源吗?对不起,我不明白你的问题。你只想加载一个帖子@user2882154no我想从所有帖子的repeater字段加载第一个图像。我有7篇帖子。所有帖子都有一个带有3或4个图像的转发器字段。我想从所有帖子的转发器字段中获取第一张图片。这里我得到了所有帖子的3到4张图片。你明白我的意思吗?好的,1秒钟我会改变我的答案