0 php meta wordpress image woocommerce
默认情况下,Woocommerce
对于alt使用的图像文件的名称.
有谁知道如何更改缩略图元(alt和标题)以显示产品名称?
试试这个 :
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;
}
Run Code Online (Sandbox Code Playgroud)
我已将 XciD 的答案更新为更清晰的版本:
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;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,在主图像上,脚本对我不起作用(XciD 都不是),但在小拇指上它是。有趣的 :)
更新:如果我关闭主图像,那么脚本将从第二个拇指开始工作!
更新 2:好的。这是一个“哦上帝,请不要!” 由于一些不好的词JS 代码改变了 alt 标签的情况。我的天啊...