0

I'm making a theme for a client in WordPress and he wants each gallery to have a grouped lightbox, so there's a lightbox for each gallery. I found out you can make it add an attribute to the gallery automatically, so what I want is an attribute called 'class' that has a number that adds by 1 for each gallery created. How would I do that?

Edit:
I see that I was not being specific enough so here's some more information: I want a 'class=""' attribute added automatically to the shortcode when the post gets created. Inside that class attribute I want it to have a number that goes up by 1 for each post containing a gallery. So the shortcode would end up like this: [gallery class="1" ...] and the next [gallery class="2" ...] and so on...

  • Do you mean native gallery shortcode? Something else? – Rarst Jun 11 '16 at 18:27
  • Yes sorry. I meant the gallery shortcode. – Mikkel Köhler Jun 11 '16 at 18:28
  • @Rarst So, you have an idea? – Mikkel Köhler Jun 11 '16 at 18:49
0

If you take a look at native galleries they already have IDs with counter implemented:

$instance++;
...
$selector = "gallery-{$instance}";
...
$gallery_div = "<div id='$selector'class='gallery...

So first gallery within a post will have <div id="gallery-1"... and so on. This is used by core itself to target different styles at each instance.

If you need to do something more custom you can use gallery_style filter, which gets passed those styles and opening div markup, to similarly keep track of instances and inject your custom classes as necessary.

  • It's not a class like that. It's a class added to the shortcode, not the div. Sorry for not being specific enough. – Mikkel Köhler Jun 11 '16 at 19:03
  • Then you should update your question with more details. Native gallery implementation doesn't allow custom classes. – Rarst Jun 11 '16 at 19:05
  • I updated my question. – Mikkel Köhler Jun 11 '16 at 19:11
  • I don't see what the point of adding this to shortcode if this is something native galleries already do (though with IDs, not classes)? – Rarst Jun 11 '16 at 19:13
  • I'm using a plugin that implements lightbox to the gallery shortcode. He made it so you can seperate them by adding classes like that to the shortcode. I'm pretty new to stackexchange and bad at explaining my problems in text. Sorry. – Mikkel Köhler Jun 11 '16 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.