1

I have a custom theme in which I have a woocommerce.php file to display the shop homepage. This is simply:

get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main">

        <div id="page-header-image">
            <?php the_post_thumbnail(); ?>
        </div>

        <?php woocommerce_content(); ?>

    </main><!-- #main -->
</div><!-- #primary -->

<?php

get_footer();

However, the_post_thumbnail(); shows the featured image of the first product on the page.

How can I show the Page featured image not the products?

Thanks

2 Answers 2

1

Thanks to you I tried this on my functions.php It worked, but also had to get the ID of the page, in my case 4.

add_action( 'woocommerce_before_main_content', 'woocommerce_category_image', 2 );
function woocommerce_category_image() {
    if ( is_product_category() ){
        global $wp_query;
        $cat = $wp_query->get_queried_object();
        $thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image = wp_get_attachment_url( $thumbnail_id );
        if ( $image ) {
            echo '<div class="category-image"><img src="' . $image . '" alt="' . $cat->name . '" /></div>';
        }
    }
    if ( is_shop() ){
        $target_post_id = '4';
        $image = wp_get_attachment_url(get_post_thumbnail_id($target_post_id));
        echo '<div class="category-image"><img src="' . $image. '" alt="' . $target_post_id->name . '" /></div>';   
    }
}
0

Well, in case it's of use to anyone else heres what I did.

I couldn't find an obvious way so I got the ID of the page I wanted to pull the featured image from then got the attachment URL from that.

I'm sure a more elegant method exists but I needed something fast. This works for the time being!

get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main">

        <?php 
        //Only show the header image if it's the shop homepage. Not product pages etc
        if ( is_shop() ):?>

            <div id="page-header-image">
                <?php 
                //Get a specific featured image based on page id. In this case the shop page which happens to be 110
                $target_post_id = '110';
                $feat_image = wp_get_attachment_url(get_post_thumbnail_id($target_post_id));
                echo '<img src="'. $feat_image.'">';
                ?>
            </div>

        <?php endif; ?>

        <?php woocommerce_content(); ?>

    </main><!-- #main -->
</div><!-- #primary -->

<?php
get_footer();
1
  • It's possible to use wc_get_page_id( 'shop' ) to get the ID of the shop page, to avoid hard-coding it. Commented Apr 19, 2018 at 1:11

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.