media - coregallery get figcaption text

I try to get gutenberg media gallery block output, and want to use default figcaption text feature for images titles (fo

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
Add a comment  | 

2 Answers 2

Reset to default 1

You’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

相关推荐

  • media - coregallery get figcaption text

    I try to get gutenberg media gallery block output, and want to use default figcaption text feature for images titles (fo

    10小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信