Ben
Ben

Reputation: 5646

Wordpress Custom Post Thumbnail Not Showing On Homepage

This is NOT a question about the featured image meta field. I've tried to find an answer but every search I've tried shows people trying to add the 'featured image' meta to their custom post type. I have that enabled for my theme and my custom-post type. It seems to work just fine. I've set a 'featured image' and I see it when I edit the post. My theme is a custom child of twentyeleven.

On my homepage I'm displaying recent entries (both 'post' type and 'custom-post' type) with the title, an excerpt, and a thumbnail to the left. The thumbnail is displaying properly for all my 'post' types, but not for my 'custom-post' type. I'm not sure where I need to look or what I need to add to get custom-post to show the thumbnail.

Adding Code: This is in the 'content.php' from my custom theme. It's mostly appropriated from twentyeleven but I think I've made some minor changes. As far as I can tell the 'if' isn't proving true for the custom post type.


      $thumbnails = get_posts('numberposts=5');
      foreach ($thumbnails as $thumbnail) {
        if ( has_post_thumbnail($thumbnail->ID) && $thumbnail->ID == $id) {
          echo 'ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';                 echo get_the_post_thumbnail( $id, 'thumbnail', array('class' => 'alignleft') );
          echo '';
        }
      }

Update: The problem looks like it's related to


    $thumbnails = get_posts('numberposts=5');

It's only pulling from the 'post' type so it can't check against my 'custom-post' type. I get the correct thumbnail when I change the code as follows, but then none of the 'post' thumbnails work.


    $args = array(
        'numberposts'     => 5,
        'post_type'       => 'pnw_picture-post');

      $thumbnails = get_posts($args);

So I guess the solution is to pull both of those types in a single query.

Upvotes: 0

Views: 2073

Answers (2)

Ben
Ben

Reputation: 5646

I took a closer look at the twentyeleven content.php - It looks like whatever I put in my child content.php is completely different. I hadn't realized I had changed so much of the code.

This is the modified working code to pull in both post types:


    $args = array(
        'numberposts'     => 5,
        'post_type' => array ( 'post', 'custom-post'));

      $thumbnails = get_posts($args);

      foreach ($thumbnails as $thumbnail) {
        if ( has_post_thumbnail($thumbnail->ID) && $thumbnail->ID == $id) {
          echo 'ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';                 echo get_the_post_thumbnail( $id, 'thumbnail', array('class' => 'alignleft') );
          echo '';
        }
      }

Upvotes: 0

ckaufman
ckaufman

Reputation: 1497

Can you post the code that you are using that you think should be returning the post thumbnail?

You may just be looking for the_post_thumbnail from the WP Codex

Upvotes: 0

Related Questions