最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Fallback media image for featured image not working in admin column

programmeradmin3浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

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' );
发布评论

评论列表(0)

  1. 暂无评论