Fallback media image for featured image not working in admin column

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);

Topic wp-list-table media-library admin Wordpress

Category Web


wp_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' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.