Wordpress acf repeater 仅获取第一张和第二张图像
Wordpress acf repeater get first and second images only
我在 wordpress 网站上使用 acf 插件。我正在使用带有显示在博客 post 上的图像子字段的转发器字段。我的目标是只获取和显示博客中的第一张和第二张图片 post 以显示在主页上。
我尝试使用 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]; ?>" />
我刚刚解决了这个问题。对于那些在转发器字段上显示一定数量的图像行也有问题的人。可以参考我的回答。
下面的代码 returns 来自转发器字段的两个图像。只是改变了条件($i>1),如果你想return一定数量的图像。
<?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; ?>
我在 wordpress 网站上使用 acf 插件。我正在使用带有显示在博客 post 上的图像子字段的转发器字段。我的目标是只获取和显示博客中的第一张和第二张图片 post 以显示在主页上。
我尝试使用 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]; ?>" />
我刚刚解决了这个问题。对于那些在转发器字段上显示一定数量的图像行也有问题的人。可以参考我的回答。 下面的代码 returns 来自转发器字段的两个图像。只是改变了条件($i>1),如果你想return一定数量的图像。
<?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; ?>