使用 ACF 图像字段在 foreach 循环中显示重复图像
Duplicate images being displayed in foreach loop with ACF image field
我正在将自定义管理 table 列添加到名为 'program' 的自定义 post 类型。
我创建了一个 acf 图像字段,它设置了数组的 return 值。
'program' 有两个 post,每个都有一个选定的图像。
另外两个 post 没有选择图像,所以我动态添加了后备图像。
图片在列中显示时,正在输出两次??
当我执行 `print_r($selected_image)` 时,我看到它在数组中输出两个 id,大写 ID 和小写 id
我正在使用 foreach 循环,因为我想从多个 post 中获取多个图像,那么 foreach 循环是否以某种方式造成了这个问题?我错过了什么吗?
代码:
function cpt_columns_content($column_id, $post_id) {
$selected_image = get_field('archive_background_image');
$size = 'xsm_thumbnail';
if ($column_id == 'last_modified') {
echo get_post_field('post_modified', $post_id);
}
else if ('post_thumbs') {
if (has_post_thumbnail()) {
the_post_thumbnail('xsm_thumbnail');
}
// foreach loop to output selected acf image field image from each post
else if (!empty($selected_image)) {
foreach ($selected_image as $image_id) {
echo wp_get_attachment_image($image_id, $size);
}
}
else if (!has_post_thumbnail()) {
echo wp_get_attachment_image(156, 'xsm_thumbnail');
//echo wp_get_attachment_image_src(156, 'archive_thumbnail')[0];
} else { ... }
}
}
add_action('manage_program_posts_custom_column', 'cpt_columns_content', 10, 2);
I see it outputs two id's in the array, uppercase ID and lowercase id
这只是默认的 WordPress 行为,它提供了两个版本,因此开发人员可以使用任一拼写来访问任何 post.
的 ID 属性
When the images are displayed in the column, they are being output twice??
因为您要遍历两个 ID...然后还要遍历更多内容。
你根本不应该在 $selected_image
上循环。那是一个单个文件的数据。您当前的代码循环遍历 ID
和 id
(因此您得到两张图像)- 然后继续循环遍历 title
、filename
等。这在总而言之,您会将值 History
作为 图像 ID 传递给 wp_get_attachment_image
接下来。
这应该只是是
echo wp_get_attachment_image($selected_image['id], $size);`
没有任何循环。
我正在将自定义管理 table 列添加到名为 'program' 的自定义 post 类型。
我创建了一个 acf 图像字段,它设置了数组的 return 值。
'program' 有两个 post,每个都有一个选定的图像。 另外两个 post 没有选择图像,所以我动态添加了后备图像。
图片在列中显示时,正在输出两次??
当我执行 `print_r($selected_image)` 时,我看到它在数组中输出两个 id,大写 ID 和小写 id
我正在使用 foreach 循环,因为我想从多个 post 中获取多个图像,那么 foreach 循环是否以某种方式造成了这个问题?我错过了什么吗?
代码:
function cpt_columns_content($column_id, $post_id) {
$selected_image = get_field('archive_background_image');
$size = 'xsm_thumbnail';
if ($column_id == 'last_modified') {
echo get_post_field('post_modified', $post_id);
}
else if ('post_thumbs') {
if (has_post_thumbnail()) {
the_post_thumbnail('xsm_thumbnail');
}
// foreach loop to output selected acf image field image from each post
else if (!empty($selected_image)) {
foreach ($selected_image as $image_id) {
echo wp_get_attachment_image($image_id, $size);
}
}
else if (!has_post_thumbnail()) {
echo wp_get_attachment_image(156, 'xsm_thumbnail');
//echo wp_get_attachment_image_src(156, 'archive_thumbnail')[0];
} else { ... }
}
}
add_action('manage_program_posts_custom_column', 'cpt_columns_content', 10, 2);
I see it outputs two id's in the array, uppercase ID and lowercase id
这只是默认的 WordPress 行为,它提供了两个版本,因此开发人员可以使用任一拼写来访问任何 post.
的 ID 属性When the images are displayed in the column, they are being output twice??
因为您要遍历两个 ID...然后还要遍历更多内容。
你根本不应该在 $selected_image
上循环。那是一个单个文件的数据。您当前的代码循环遍历 ID
和 id
(因此您得到两张图像)- 然后继续循环遍历 title
、filename
等。这在总而言之,您会将值 History
作为 图像 ID 传递给 wp_get_attachment_image
接下来。
这应该只是是
echo wp_get_attachment_image($selected_image['id], $size);`
没有任何循环。