WordPress – Featured image on a custom post archive page

i have created a custom post named Products.

register_post_type( 'products',
    array(
        'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' )
        ),
    'public' => true,
    'has_archive' => true,
    'supports' => array( 'title', 'editor', 'thumbnail' )
);

I have also created a php file named archive-products.php and made it into a template.

Read More

In WordPress I have created a page named Products and selected the products template.

On that static page (that uses the archive template) I have uploaded a image into the Featured Image panel.

In my header I have the code:

echo get_the_post_thumbnail();

But this echos the Featured image of the last custom post in the list (all the products posts have a featured image as well), not the Featured image of the static/archive page, which is what i want. Is this possible to achieve?

Thanks!

Related posts

Leave a Reply

1 comment

  1. I did the very same thing and came across the following answer that solved my problem: https://wordpress.stackexchange.com/a/175228

    1. Save your custom post type archive template as a page.

      For example, page-products.php

    2. Backup locally and delete your custom post type archive template from your server.

    3. Show the image with the_post_thumbnail(), put it in a variable with get_the_post_thumbnail(), or make it a background image with your page title over it:

      $bg = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' );
      if( is_page('products') ) : ?>
      <div style="background: url('<?php echo $bg[0]; ?>') repeat center center #fbfbfb; background-size:cover;">
      <?php the_title( '<h1 class="page-title">', '</h1>' ); ?>
      </div>
      <?php endif; ?>

    4. Save your permalinks and refresh your page.

    This is what worked for me. Hope it helps somebody. 🙂