Custom function alt attribute images - by Rashkoff

This topic has 6 replies, 2 voices, and was last updated 4 years, 10 months ago ago by Olga Barlow

  • Avatar: Ricky_Rashkoff
    Rashkoff
    Participant
    February 1, 2020 at 23:01

    Hello
    I’m trying to overwrite the alt and title attributes of images
    I can’t insert the brand name (text) in output.
    It’s return the value “Array”
    Will be nice you can help me with this.
    Thanks

    this is my code

    add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
    
    function change_attachement_image_attributes( $attr, $attachment ){
    
    	global $post, $product;
    	
    	// 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);
     	$sku = get_post_meta( $post->ID, '_sku', true );
    //	$brand = get_post_meta( $post->ID, '_et_primary_category', true );
    	$brand = wp_get_post_terms( $post->ID, 'brand', array( 'fields' => 'all' ));
    	
    
        $attr['alt'] = $sku . ' | ' . $title . ' | ' . $brand;
        $attr['title'] = $title;
    
        return $attr;
    }
    5 Answers
    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    February 3, 2020 at 16:07

    Hello,

    Because you are using function https://developer.wordpress.org/reference/functions/wp_get_post_terms/ that returns array https://prnt.sc/qwx39w and you get it https://prnt.sc/qwx3px So, everything works according to your code. If you need help with additional customization you can submit request here. Default support does not include customization service.

    Regards

    Avatar: Ricky_Rashkoff
    Rashkoff
    Participant
    February 3, 2020 at 16:55

    Hello,
    is it possible to help me to fix the code and return the name of the category of the product?
    Thanks

    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    February 3, 2020 at 17:26

    Hello,

    If you need help with additional customization you can submit request here. Default support does not include customization service we can help you only with the existing options.

    Regards

    Avatar: Ricky_Rashkoff
    Rashkoff
    Participant
    February 6, 2020 at 19:01

    I need to know how return a the name of the brand in php variable
    see private massage
    thanks

    Please contact administrator
    for this information.
    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    February 7, 2020 at 11:05

    Hello,

    Try to replace this line in your code
    $attr['alt'] = $sku . ' | ' . $title . ' | ' . $brand;

    by the following one

    foreach( $brand as $brand_item ) {
    		$attr['alt'] = $sku . ' | ' . $title . ' | ' . $brand_item->name;
    }

    Regards

  • Viewing 6 results - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.