filters - Add Index to Item in Wordpress Gallery

I'm creating a gallery of images using the [gallery] shortcode and Jetpack's tiled gallery feature. When the u

I'm creating a gallery of images using the [gallery] shortcode and Jetpack's tiled gallery feature. When the user clicks on an image I'm triggering a carousel (Owl Carousel) with carousel.trigger( 'to.owl.carousel', [ item_id, 0 ] ). The problem that I have is that I need to feed the trigger an index in order to have it jump to the correct item. I've previously done this by using jQuery index() but that won't work with the tiled gallery because items are nested inside rows.

What I need to do is add a data-index property to each item in the tiled gallery which I can then have correspond to the index in the carousel. Is there a way in which I can filter the gallery shortcode output in order to add this attribute as it loops through the images?

Off topic but an alternative solution might be leveraging index() to index the items regardless of their nested row structure, like indexing .item elements in relation to the parent of parent of parent container. Is that possible?

I'm creating a gallery of images using the [gallery] shortcode and Jetpack's tiled gallery feature. When the user clicks on an image I'm triggering a carousel (Owl Carousel) with carousel.trigger( 'to.owl.carousel', [ item_id, 0 ] ). The problem that I have is that I need to feed the trigger an index in order to have it jump to the correct item. I've previously done this by using jQuery index() but that won't work with the tiled gallery because items are nested inside rows.

What I need to do is add a data-index property to each item in the tiled gallery which I can then have correspond to the index in the carousel. Is there a way in which I can filter the gallery shortcode output in order to add this attribute as it loops through the images?

Off topic but an alternative solution might be leveraging index() to index the items regardless of their nested row structure, like indexing .item elements in relation to the parent of parent of parent container. Is that possible?

Share Improve this question asked Apr 25, 2019 at 12:13 Kevin NugentKevin Nugent 5631 gold badge8 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

So I've solved the issue by looping through all of the items by class and indexing them with a data attribute.

$( '.tiled-gallery').each(function(){

    var i = 0;

    $(this).find('.tiled-gallery-item').each(function(){

        $(this).attr( 'data-index', i );

        i++;
    })
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745552922a4632660.html

相关推荐

  • filters - Add Index to Item in Wordpress Gallery

    I'm creating a gallery of images using the [gallery] shortcode and Jetpack's tiled gallery feature. When the u

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信