問題の概要
Visual Composerでは、Single Image、Image Gallery、Image Masonry Gallery、Simple Image Slider、Feature Description要素内の画像のキャプションとaltテキストは翻訳に送信されません。翻訳されたページでは、これらはデフォルト言語で表示されます。
回避策
次のいずれかのオプションを使用してください。
- Single Image要素の代わりに、画像とそのキャプションおよび代替テキストをテキストブロック要素に追加します。テキストブロックのコンテンツは、キャプションと代替テキストを含めて翻訳に送信されます。
- または、お使いのテーマのfunctions.phpファイルにこのコードを追加し、ページを再度翻訳に送信します。これにより、Visual Composer要素内の画像のキャプションと代替テキストが翻訳に送信されます。これはSingle Image要素でテスト済みです:
add_filter( 'wpml_tm_translation_job_data', function ( $package ) {
if ( empty( $package['contents']['field-vcv-pageContent-0']['data'] ) ) {
return $package;
}
$page = json_decode( rawurldecode( base64_decode( $package['contents']['field-vcv-pageContent-0']['data'] ) ), true );
foreach ( (array) ( $page['elements'] ?? [] ) as $element_id => $element ) {
foreach ( (array) $element as $attribute => $items ) {
if ( ! is_array( $items ) ) {
continue;
}
foreach ( $items as $index => $item ) {
foreach ( [ 'caption', 'alt' ] as $key ) {
if ( ! is_array( $item ) || empty( $item[ $key ] ) || ! is_string( $item[ $key ] ) ) {
continue;
}
$field = 'vcv-pageContentField--' . $element_id . '.' . $attribute . '.' . $index . '.' . $key;
$package['contents'][ 'field-' . $field . '-0' ] = [ 'translate' => 1, 'data' => base64_encode( $item[ $key ] ), 'format' => 'base64' ];
$package['contents'][ 'field-' . $field . '-0-name' ] = [ 'translate' => 0, 'data' => $field ];
$package['contents'][ 'field-' . $field . '-0-type' ] = [ 'translate' => 0, 'data' => 'custom_field' ];
}
}
}
}
return $package;
}, 10 );