如何更改目录缩略图,产品缩略图中的元(alt和标题)? - Woocommerce

时间:2014-11-23 10:07:48

标签: php wordpress image woocommerce meta

默认情况下,Woocommerce用于alt使用过的图片文件的名称。

有谁知道如何更改缩略图元(alt和标题)以显示产品名称

2 个答案:

答案 0 :(得分:5)

试试这个:

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;
}

答案 1 :(得分:3)

我已经更新了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;
}   

不幸的是,在主图像上,脚本对我来说并不起作用(XciD也没有),但是在它的小拇指上。有意思:))

更新:如果我关闭主图像,那么脚本将从第二个拇指开始工作!

更新2:好的。这是一个"哦,上帝请不!"一些坏词的情况 JS代码改变了alt标签。 OMG ...

相关问题