admin管理员组文章数量:1130349
Adding alt tag information to every product photo is a lot of work. We always just copy and paste our product title into the image alt tags.
I figured since all the information is there; there must be a way to do this automatically.
Question: How do you apply a woocommerce product's TITLE as all the ALT TAGS for images used with that product.
Any help is much appreciated!
Adding alt tag information to every product photo is a lot of work. We always just copy and paste our product title into the image alt tags.
I figured since all the information is there; there must be a way to do this automatically.
Question: How do you apply a woocommerce product's TITLE as all the ALT TAGS for images used with that product.
Any help is much appreciated!
Share Improve this question edited Feb 26, 2017 at 9:58 Patrick asked Feb 26, 2017 at 9:13 PatrickPatrick 2953 gold badges10 silver badges26 bronze badges 1- This is what you want mate, already been asked before :) stackoverflow/questions/27087772/… – Richard Webster Commented Feb 26, 2017 at 9:31
3 Answers
Reset to default 9This is what you need, taken from - https://stackoverflow/questions/27087772/how-can-i-change-meta-alt-and-title-in-catalog-thumbnail-product-thumbnail
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;
}
For anyone searching, I would recommend editing the code above so if a product image already has an alt tag do not override it with the post title. This way you can still add product image titles if necessary.
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);
if( $attr['alt'] == ''){
$attr['alt'] = $title;
$attr['title'] = $title;
}
return $attr;
}
If anyone is looking for a fast and easy way to use product titles as product image alt tags, I recommend the Woo Image SEO plugin.
The plugin can also handle the creation of title attributes.
Additionally, you can customize the attributes by including the product's category and tag in any order.
Adding alt tag information to every product photo is a lot of work. We always just copy and paste our product title into the image alt tags.
I figured since all the information is there; there must be a way to do this automatically.
Question: How do you apply a woocommerce product's TITLE as all the ALT TAGS for images used with that product.
Any help is much appreciated!
Adding alt tag information to every product photo is a lot of work. We always just copy and paste our product title into the image alt tags.
I figured since all the information is there; there must be a way to do this automatically.
Question: How do you apply a woocommerce product's TITLE as all the ALT TAGS for images used with that product.
Any help is much appreciated!
Share Improve this question edited Feb 26, 2017 at 9:58 Patrick asked Feb 26, 2017 at 9:13 PatrickPatrick 2953 gold badges10 silver badges26 bronze badges 1- This is what you want mate, already been asked before :) stackoverflow/questions/27087772/… – Richard Webster Commented Feb 26, 2017 at 9:31
3 Answers
Reset to default 9This is what you need, taken from - https://stackoverflow/questions/27087772/how-can-i-change-meta-alt-and-title-in-catalog-thumbnail-product-thumbnail
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;
}
For anyone searching, I would recommend editing the code above so if a product image already has an alt tag do not override it with the post title. This way you can still add product image titles if necessary.
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);
if( $attr['alt'] == ''){
$attr['alt'] = $title;
$attr['title'] = $title;
}
return $attr;
}
If anyone is looking for a fast and easy way to use product titles as product image alt tags, I recommend the Woo Image SEO plugin.
The plugin can also handle the creation of title attributes.
Additionally, you can customize the attributes by including the product's category and tag in any order.
本文标签: phpHow to automatically apply woocommerce product title to all product images alt tags
版权声明:本文标题:php - How to automatically apply woocommerce product title to all product images alt tags? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749072816a2311843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论