0

I have the following snippet which adds post thumbnails to products on archive page if matches a given product category, which will output the image but echos the the post id as well. How do I suppress to not show post id?

add_action( 'woocommerce_after_shop_loop_item_title', function () {
    if(is_product_category('t-shirts')) {
        $thumb = get_the_post_thumbnail( the_ID());
        if(is_string($thumb) ) {
            echo '<div class="imagewrapper">' . $thumb;
        }
    }

}, 9 );
add_action( 'woocommerce_after_shop_loop_item_title', function () {
    if(is_product_category('t-shirts')) {
        $thumb = get_the_post_thumbnail( the_ID());
        if(is_string($thumb) ) {
            echo '</div>';
        }
    }
}, 11 );
1

I believe because:

the_ID() : Display the ID of the current item in the WordPress Loop.

and

get_the_ID() : Retrieve the ID of the current item in the WordPress Loop.

So the_ID() displays the post ID.
Try to use get_the_ID()

  • Yep thanks a lot! I just took a look on the 2 methods /** * Display the ID of the current item in the WordPress Loop. * * @since 0.71 */ function the_ID() { echo get_the_ID(); } /** * Retrieve the ID of the current item in the WordPress Loop. * * @since 2.1.0 * * @return int|false The ID of the current item in the WordPress Loop. False if $post is not set. */ function get_the_ID() { $post = get_post(); return ! empty( $post ) ? $post->ID : false; } – fefe Jun 17 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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