How can i change meta (alt and title) in catalog thumbnail , product thumbnail ? - Woocomerce

前端 未结 2 1225
情话喂你
情话喂你 2020-12-29 17:25

By default in Woocommerce for alt used image file\'s name.

Does anyone know how to change thumbnail meta (alt and title) to show Product name

相关标签:
2条回答
  • 2020-12-29 18:11

    I've updated XciD's answer to a much cleaner version:

    add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
    function change_attachement_image_attributes($attr, $attachment) {
        global $post;
        if ($post->post_type == 'product') {
            $title = $post->post_title;
            $attr['alt'] = $title;
            $attr['title'] = $title;
        }
        return $attr;
    }   
    

    Unfortunately on the main image the script doesn't work for me (XciD's neither), but on the small thumbs it is. Interesting :)

    Update: If I turn off the main image, then the script starts working from the second thumb!

    Update 2: Ok. It was an "Oh God Please No!" situation as some bad words JS code changed the alt tag. OMG...

    0 讨论(0)
  • 2020-12-29 18:20

    Try this :

    add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
    
    function change_attachement_image_attributes( $attr, $attachment ){
        // Get post parent
        $parent = get_post_field( 'post_parent', $attachment);
    
        // Get post type to check if it's product
        $type = get_post_field( 'post_type', $parent);
        if( $type != 'product' ){
            return $attr;
        }
    
        /// Get title
        $title = get_post_field( 'post_title', $parent);
    
        $attr['alt'] = $title;
        $attr['title'] = $title;
    
        return $attr;
    }
    
    0 讨论(0)
提交回复
热议问题