我在wordpress网站上使用acf插件。我正在使用带有显示在博客文章中的图像子字段的中继器字段。我的目标是仅获取并显示博客文章中要为主页显示的第一张和第二张图像。
我尝试使用 acf 站点文档中的此代码,但无济于事,代码不起作用。有人知道这个问题吗?
<?php while ($the_query -> have_posts()) : $the_query ->
the_post();
$postqID = get_the_ID();
?>
<?php
$rows = get_field('post_images', $postqID ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['post_image' ]; // 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]; ?>" />
我刚刚解决了这个问题。对于那些在中继器字段上显示一定数量的图像行时也遇到问题的人。你可以参考我的答案。下面的代码从中继器字段中返回两个图像。刚刚更改了条件($i>1),如果您希望返回一定数量的图像。
<?php
$i = 0;
if(get_field('post_images', $postqID)):
?>
<?php while (has_sub_field('post_images',$postqID)): ?>
<img src="<?php the_sub_field('post_image')['url']; ?>"></img>
<?php
$i++;
if($i > 1)
{
break;
}
?>
<?php endwhile; ?>
<?php endif; ?>