1

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

1 Answer 1

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' );
8
  • rudtek, how can I just display the image without using html here?
    – Krys
    Commented Mar 9, 2022 at 1:20
  • rudtek, this is suppose to be the fallback image, if no feature image has been made
    – Krys
    Commented Mar 9, 2022 at 1:21
  • Yep. Got that. My question is what if your client deletes this fallback image from the media libarary? Then what? That's why I always code mine so they can't. (ie in the theme or plugin). I've updated my answer to address your "no html" question as well.
    – rudtek
    Commented Mar 9, 2022 at 2:57
  • rudtek, if I was to use an image from my theme image directory as a fallback image, how can I output it without wrapping in html img tags? I have this so far, but its only outputting the url: get_stylesheet_directory_uri() .'/assets/images/geo.jpg';
    – Krys
    Commented Mar 13, 2022 at 6:59
  • what do you have against HTML?
    – rudtek
    Commented Mar 13, 2022 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.