Tự động thêm thẻ ALT ảnh theo Title bài đăng

Tự động thêm thẻ ALT ảnh theo Title bài đăng

Để sử dụng bạn copy đoạn code sau vào file functions.php trong thư mục theme bạn đang sử dụng nhé!

add_filter('wp_get_attachment_image_attributes', 'change_attachment_image_attributes', 20, 2);
function change_attachment_image_attributes( $attr, $attachment ){
    $parent = get_post_field( 'post_parent', $attachment);
    $type = get_post_field( 'post_type', $parent);
    if( $type != 'product' ){
        return $attr;
    }
    $title = get_post_field( 'post_title', $parent);
    $attr['alt'] = $title;
    $attr['title'] = $title;
    return $attr;
}

Đoạn code trên rất thuận lợi cho viết bài SEO, tiết kiệm thêm thời gian

Chúc các bạn thành công!