I want to display a fallback image if a custom post type does not have a featured image set and displayed in a custom admin table column.
However all that is being output is the image url and not the image from the media library.
I have used wp_get_attachment_image_src()
to output the image, though I may not be going about it correctly?
// fill column header content
function cpt_columns_content($column_id, $post_id) {
if ($column_id == 'last_modified') {
echo get_post_field('post_modified', $post_id);
} else if ('post_thumbs') {
the_post_thumbnail('xsm_thumbnail');
// not sure this is the correct way to go?
} if (!has_post_thumbnail()) {
// 156 is the specific media image id
echo wp_get_attachment_image_src(156, 'archive_thumbnail')[0];
}
}
add_action('manage_professor_posts_custom_column', 'cpt_columns_content', 10, 2);
add_action('manage_campus_posts_custom_column', 'cpt_columns_content', 10, 2);
I want to display a fallback image if a custom post type does not have a featured image set and displayed in a custom admin table column.
However all that is being output is the image url and not the image from the media library.
I have used wp_get_attachment_image_src()
to output the image, though I may not be going about it correctly?
// fill column header content
function cpt_columns_content($column_id, $post_id) {
if ($column_id == 'last_modified') {
echo get_post_field('post_modified', $post_id);
} else if ('post_thumbs') {
the_post_thumbnail('xsm_thumbnail');
// not sure this is the correct way to go?
} if (!has_post_thumbnail()) {
// 156 is the specific media image id
echo wp_get_attachment_image_src(156, 'archive_thumbnail')[0];
}
}
add_action('manage_professor_posts_custom_column', 'cpt_columns_content', 10, 2);
add_action('manage_campus_posts_custom_column', 'cpt_columns_content', 10, 2);
Share
Improve this question
asked Mar 8, 2022 at 15:49
KrysKrys
1353 silver badges9 bronze badges
1 Answer
Reset to default 1wp_get_attachment_image_src(156, 'archive_thumbnail')[0]
will only echo the URL of the image. You need to add it to html to display the image. Try this:
echo '<img src="'.wp_get_attachment_image_src(156, 'archive_thumbnail')[0].'"/>';
You could add the width etc as well
echo '<img src="'.wp_get_attachment_image_src(156, 'archive_thumbnail')[0].'" width=".wp_get_attachment_image_src(156, 'archive_thumbnail')[1]." height=".wp_get_attachment_image_src(156, 'archive_thumbnail')[2]." />';
what happens if someone deletes this image though? Do you have a fallback?
It may be better to add the fallback image to your theme or plugin directory and reference it that way.
UPDATE If you just want to add the image as simply as possible. You could also try this:
wp_get_attachment_image( 156, 'archive_thumbnail' );