I try to get gutenberg media gallery block output, and want to use default figcaption text feature for images titles (for js lightbox). How can i get this property figcaption text value?
add_filter( 'render_block', function( $block_content, $block ) {
if ( 'core/gallery' !== $block['blockName'] || ! isset( $block['attrs']['ids'] ) ) {
return $block_content;
}
$li = '';
foreach( (array) $block['attrs']['ids'] as $id ) {
$li .= sprintf( '<li><a href="%s" data-lightbox="photo" data-title="%s"><img src="%s" class="w-100"></a></li>',
wp_get_attachment_image_src( $id, '' )[0],
'FIGCAPTION TEXT',
wp_get_attachment_image_src( $id, 'thumbnail' )[0] );
}
return sprintf( '<ul id="gallery-grid">%s</ul>', $li );
}, 10, 2 );
I try to get gutenberg media gallery block output, and want to use default figcaption text feature for images titles (for js lightbox). How can i get this property figcaption text value?
add_filter( 'render_block', function( $block_content, $block ) {
if ( 'core/gallery' !== $block['blockName'] || ! isset( $block['attrs']['ids'] ) ) {
return $block_content;
}
$li = '';
foreach( (array) $block['attrs']['ids'] as $id ) {
$li .= sprintf( '<li><a href="%s" data-lightbox="photo" data-title="%s"><img src="%s" class="w-100"></a></li>',
wp_get_attachment_image_src( $id, '' )[0],
'FIGCAPTION TEXT',
wp_get_attachment_image_src( $id, 'thumbnail' )[0] );
}
return sprintf( '<ul id="gallery-grid">%s</ul>', $li );
}, 10, 2 );
Share
Improve this question
asked Jun 17, 2019 at 15:15
MikeMike
112 bronze badges
2 Answers
Reset to default 1You’re right...the figcaptions are encoded as html in $block['innerHTML']
. You could do sth. like $tmpArray = explode('</li>',$block['innerHTML']);
before your foreach
loop to split the HTML string into an array that matches your gallery items and inside the loop strip_tags($tmpArray[i]);
to strip away all html tags and only get the text string inside <figcaption></figcaption>
. You’ll only need to add a counter i
to your loop to have an index for the $tmpArray
.
Try wp_get_attachment_caption( $id )
for caption and get_post_meta( $id, '_wp_attachment_image_alt', true )
for alt-text
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745391342a4625673.html
评论列表(0条)