I am adding a custom admin table column to a custom post type called 'program'.
I created an acf image field, that is set with a return value of array.
'program' has two posts that have a selected image each. The other two posts have no image selected, so I have added a fallback image dynamically.
When the images are displayed in the column, they are being output twice??
When I do `print_r($selected_image)` I see it outputs two id's in the array, uppercase ID and lowercase id
I am using a foreach loop, since there are multiple images that I want to get from more than one post, so is the foreach loop creating this issue somehow? am I missing something?
Code:
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);
CodePudding user response:
I see it outputs two id's in the array, uppercase ID and lowercase id
That is just default WordPress behavior, it provides both versions, so that developers can use either spelling to access the id property of any post.
When the images are displayed in the column, they are being output twice??
Because you are looping over both IDs ... and then even more stuff.
You should not be looping over $selected_image
at all here. That is the data for one single file. Your current code loops over both ID
and id
(hence you get two images) - and would then proceed to loop over title
, filename
etc. That makes no sense at all, you would be passing the value History
as an image id to wp_get_attachment_image
next.
This should just be
echo wp_get_attachment_image($selected_image['id], $size);`
without any loops.