This topic has 6 replies, 2 voices, and was last updated 4 years, 9 months ago ago by Olga Barlow
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;
}
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
Hello,
is it possible to help me to fix the code and return the name of the category of the product?
Thanks
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
I need to know how return a the name of the brand in php variable
see private massage
thanks
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
You must be logged in to reply to this topic.Log in/Sign up