Francis
Francis

Reputation: 857

Custom post type featured image not showing

I have checked out the answers in Featured Images in WP not showing and Custom Posts on Wordpress not showing. I get the title and permalink of my CPT UI posts, but not the featured image.

I added the following at the top of functions.php:

function custom_theme_features()  {
    // Add theme support for Featured Images
    add_theme_support( 'post-thumbnails' );
}
// Hook into the 'after_setup_theme' action
add_action( 'after_setup_theme', 'custom_theme_features' );

I checked the "featured image" option at the bottom of the Edit Post Type windows of CPT UI

Here is the code. The comments show what works and what does not:


$recent_posts = wp_get_recent_posts(array('post_type' => 'artwork', 'numberposts' => '10'));
foreach ($recent_posts as $recent) {
    $recentID =  $recent['ID'] ;
    echo ' --- recentID=' . $recentID;  // returns the ID, OK
    echo ' --- post_title=' . $recent["post_title"]; // returns the custom post title, OK
    echo ' --- get_permalink=' .   get_permalink($recentID) ; // returns the right permalink, OK
    echo ' --- get_post_thumbnail_id=' . get_post_thumbnail_id($recentID); // returns 0
    echo ' --- get_post_thumbnail_id=' . get_post_thumbnail_id($recent); // return nothing
    echo ' --- acf_get_post_thumbnail=' . acf_get_post_thumbnail($recentID)['url'] ; // return nothing (trying anything)
    echo ' --- get_url_from_thumbnail_id=' . get_url_from_thumbnail_id($recentID); // returns nothing
                        
    the_post_thumbnail(); // returns the same image several times. the image is a featured image of a post of a non-custom post

    echo ' --- wp_get_attachment_image='.wp_get_attachment_image($attachment_id, 'thumbnail'); // returns nothing 
            }
                    

Upvotes: 1

Views: 683

Answers (1)

Francis
Francis

Reputation: 857

Thanks to @Caleb and @GrafiCode for your answers.

The problem that was blocking me was quite simple: the custom type posts didn't have featured images, but a field called 'featured_image_field' that I myself created. So it turns out that I don't need the featured image at all, just the value of the field, that I can get with the code

get_field('featured_image_field', $recentID);

Upvotes: 1

Related Questions