Home > Mobile >  PHP: Get image to link to Product id (woocommerce)
PHP: Get image to link to Product id (woocommerce)

Time:11-02

Plugin to flip product images on hover. It works to flip to second image, but images are no longer clickable/linking to their respective product. Please help!

Original Code:

$output = '<div >';
                //$post->post_title;
                //$image_url_top = get_the_post_thumbnail_url($post->ID, 'woocommerce_thumbnail');
                $image_url_bottom = wp_get_attachment_image_src($get_gallery_image_ids[0], 'woocommerce_thumbnail' );
                $output .= '<img  src="'.$image_url_top.'" alt="'.$image_top_alt.'" />';
                //$output .= '<img  width="300" height="300" src="'.$image_url_bottom[0].'" />';
                $output .= '<img  src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" />';
                        
            $output .= '</div>';

I've Tried wrapping output of second image in a link, which I managed, but I cant get that link to call the product ID and create the correct link.

I've succesfully linked like this:

 $output .= '<a href="#"><img  src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" /></a>';

And then I've unsuccessfully tried adding the following in place of the "#" above in four different attempts (Newbie with PHP):

"get_permalink($product_id)"

and

<?php echo get_permalink($product_id) ?>

and

<?php echo woocommerce_get_the_permalink() ?>

and

<?php echo $rows[$product_buy];?>

No luck....any ideas?

CodePudding user response:

Try the below code.

$output .= '<a href="'.get_permalink( $post->ID ).'"><img  src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" /></a>';

OR

$output .= '<a href="'.get_permalink( $product->get_id() ).'"><img  src="'.$image_url_bottom[0].'" alt="'.$image_bottom_alt.'" /></a>';
  • Related