Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1249331

    As topic title say. My sitemap only shows 1 image for woo commerce product that have more then 1 image. I have added this code to function.php, but it does not help.

    • This topic was modified 5 years, 3 months ago by snitt.
    • This topic was modified 2 weeks, 5 days ago by Yigit.
    • This topic was modified 2 weeks, 5 days ago by Yigit.
    #1250544

    Hey snitt,

    Thank you for the inquiry.

    The filters above will only extract the image IDs in the builder elements or shortcodes. They will not be able to get the images in the product gallery.

    We have to get the IDs of the product gallery items, get the image info based on the image ID and include the collected info in the image array attached to the product. To do that we have to adjust the code a bit. Please try this snippet in the functions.php file instead.

    add_filter('wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages_for_products', 10, 2);
    function avia_filter_wpseo_sitemap_urlimages_for_products($images, $postid) {
    	$product = new WC_product($postid);
    	$attachment_ids = $product->get_gallery_image_ids();
    
    	foreach( $attachment_ids as $attachment_id ) 
    	{
    		$title = get_the_title($attachment_id);
    		$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
    		$src = wp_get_attachment_url($attachment_id);
    		$images[] = array('src' => $src, 'title' => $title, 'alt' => $alt);
    	}
    
    	return $images;
    }
    

    Best regards,
    Ismael

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.