Skip Navigation

[Resolved] Creating multiple galleries with lightbox

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 17 replies, has 2 voices.

Last updated by NellyK1283 4 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1474657

Dear Toolset team,

I have a custom post type of events and there is a gallery field (image field with multiple instances allowed) in each post. Here is an example of such a post:
hidden link

I created a separate page with a grid, containing featured image thumbnail and event title of each post that has images, uploaded in the gallery field. I achieved that using conditional output. Here it is:
hidden link

So far so good bu here come the difficulties I have experienced. I need to make the featured image and the title a link to the post gallery. Once a visitor clicks on the featured image or a title, it should open a lightbox gallery with all the images, uploaded to that repeater field.

I checked your documentation and the community as well but was not able to find a solution for that specific case. Can you please provide some help?

Thank you in advance!

#1475043

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Nelly

I visited that link (hidden link) and I'm not sure what I'm looking at.

Let me see if I have understood your question correctly.

You have a post type that has a featured image, and also has an image field that accepts multiple values.

In the template to display the post type, you want to display the featured image, but you don't want to display the other images. You only want those images to be seen in a lightbox once someone clicks the featured image, which would open the lightbox, and then people can navigate to the other images within the lightbox, but they otherwise do not see them.

Is that right?

#1475579

Hi Nigel,

Please excuse me for not explaining clearly what I'm trying to achieve!

As I said I have a custom post type for events. Here is a link to such an event:
hidden link
Everything is fine here and I do not need any assistance about it. Please note the gallery in the right sidebar.

I have another page, called Multimedia Albumi (which means Galleries) where I need to pull automatically the featured image of the events posts that have images uploaded in the gallery field. At the moment we have just two posts that fulfil these criteria.
I almost achieved that too using conditional output:
hidden link

What I need to do next is to make that featured image a link to a lightbox gallery that displays the images, uploaded to the image field of each post. They should be displayed in a lightbox with left/right arrow keys.

So this page should be a collection of all the images of all the events.

Hope my new explanation is good enough but please do not hesitate to ask for more info if needed!

Thank you in advance!

#1476645

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Nelly

OK, I think I've got it.

Toolset doesn't include a lightbox, but WordPress itself uses Thickbox. It's not the fanciest of lightbox implementations, and you may have a preference for another. I'll describe the solution using thickbox, and if necessary you can adapt it for another lightbox, the principles should be the same.

Any lightbox solution will need its JavaScript and CSS assets enqueuing. With thickbox that is pretty easy because WordPress has a function for that, otherwise you will need to enqueue the assets yourself (see https://developer.wordpress.org/themes/basics/including-css-javascript/).

With thickbox you can add a function like this to enqueue the files (e.g. as a snippet at Toolset > Settings > Custom Code):

add_action('template_redirect', 'enqueue_thickbox');
function enqueue_thickbox() {
   if ( true ){
       add_thickbox();
   }
}

(I've added a meaningless condition to that code—if true—which you might want to replace with conditions for which pages the lightbox files are enqueued on, e.g. using is_page (https://developer.wordpress.org/reference/functions/is_page/).

Now to the View that sets up displaying lightboxes of the custom fields when the featured images are clicked on.

You are using a conditional shortcode to only show featured images of posts which have a value set for the image fields, and I think you have that working. (On my test site I'm using a simpler solution based upon the fact that if you order by a custom field and there is no value for that custom field, the post will be omitted from the results.)

Now to output of the View:

		<wpv-loop>
          <a href='[wpv-post-featured-image size="large" output="url"]' class="thickbox" rel="group-[wpv-post-id]">
			[wpv-post-featured-image]
		  </a>
          [wpv-for-each field='wpcf-slides']
			<a class="thickbox" href="[types field='slides' output='raw'][/types]" rel="group-[wpv-post-id]">
			</a>
		  [/wpv-for-each]
		</wpv-loop>

The requirements for thickbox are that you output an image which itself is wrapped in a link to a full-sized version of the image and which has the class "thickbox". To group images into a gallery that can be navigated you also need to add the same rel attribute to all of the images that should be in the same group.

So, you can see how the featured image is output, and a rel attribute is dynamically generated (using the current post ID) for each post so that when we also add the custom field images they will be grouped together with their matching featured image.

The next part, using the wpv-for-each shortcode, is used to iterate over each image added as a custom field to a post (because the image field can have multiple values).

Note that we have to specify the normal Types prefix of "wpcf-" when specifying the custom field (which in my example has a slug of "slides").

Also, you don't want to show the custom field images initially, you only want to show them in the lightbox once the featured image has been clicked.

So I have added the links to the images, but the links themselves are empty, they do not contain the image tag itself, which I'm doing because the lightbox plugin requires the links for the lightbox to work.

I suggest you implement the same on your site with thickbox, and then if you want to try a different lightbox you then modify the above accordingly.

#1477703

Hi Nigel,

Thank you for your response! The solution you provided worked very well! I tested it with Thickbox and with Fancy Box plugin - it did the job in both cases.

The only issue I met is that conditional output shortcode causes some problems - only one featured image has been displayed. Once I remove the conditional output everything is fine. Here you can take a look:
hidden link

So can you please help me with filtering? In your previous reply you said:
"On my test site I'm using a simpler solution based upon the fact that if you order by a custom field and there is no value for that custom field, the post will be omitted from the results."
Can you please tell me how to achieve the filtering following your solution. I'm not sure that I got it.

Thank you very much for your helpful assistance!

#1480393

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Here is what I understood as the requirement: "to only show featured images of posts which have a value set for the image fields".

So, if a post has some values for the custom image field, show that post (its featured image).

If the post doesn't have any custom image field values, skip it.

That's what you want, right?

My solution relied on a peculiarity of how WordPress works. If you query for posts and specify that they should be ordered by some custom field, if a post doesn't have a value for that custom field then it will be omitted from the results, which is what I understood you to want.

But the ordering may then not be want you want, in which case you would need to persist with using the conditional shortcode.

Where you output the fields (the featured image and markup for the lightbox) wrap them in a wpv-conditional shortcode which simply tests the image field against an empty string.

#1480523

Dear Nigel,

Thank you for your response! This is exactly what I want to achieve, please excuse me if my English is not polished enough to describe clearly my needs.

Unfortunately, the result I got is correct only for the first post with custom image field not-empty and not for the rest. I believe I didn't get the idea right, so some further assistance will be very helpful.

Here is my code in the Templates for this View section:

[wpv-conditional if="( !empty($(wpcf-galeria)) )"]
<wpv-loop>
  <a href='[wpv-post-featured-image size="large" output="url"]'  rel="group-[wpv-post-id]">
    [wpv-post-featured-image size="large"]
  </a>
  [wpv-for-each field='wpcf-galeria']
    <a href="[types field='galeria' output='raw'][/types]" rel="group-[wpv-post-id]">
    </a>
  [/wpv-for-each]
</wpv-loop>
<div class="multimedia-gallery-title">[wpv-post-title]</div>
<div class="multimedia-gallery-date">[wpv-post-date]</div>
[/wpv-conditional]

And here is the result:
hidden link

I can see 2 issues here:
- Only the featured image of the first post (that contains images in the custom field) has been displayed;
- First and last columns are empty.

So can you please tell me what I'm doing wrong?

Thank you so much for your help!

#1480769

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Your conditional shortcode needs to be inside the wpv-loop tags.

Where you have it now, the shortcode is evaluated once only.

Move it inside the wpv-loop tags. When the View is displayed it iterates over the resulting posts and outputs everything inside the wpv-loop tags each time.

You need to check for every post in the results whether that post has values for the image custom fields.

#1480793

I tried it before my previous reply but I'm afraid I still can not get the desired result:
hidden link

Here is the code as it looks now:

<wpv-loop>
  [wpv-conditional if="( !empty($(wpcf-galeria)) )"]
  <a href='[wpv-post-featured-image size="large" output="url"]'  rel="group-[wpv-post-id]">
    [wpv-post-featured-image size="large"]
  </a>
  [wpv-for-each field='wpcf-galeria']
    <a href="[types field='galeria' output='raw'][/types]" rel="group-[wpv-post-id]">
    </a>
  [/wpv-for-each]
  [/wpv-conditional]
</wpv-loop>
<div class="multimedia-gallery-title">[wpv-post-title]</div>
<div class="multimedia-gallery-date">[wpv-post-date]</div>

I also tried to place the following two lines:

<div class="multimedia-gallery-title">[wpv-post-title]</div>
<div class="multimedia-gallery-date">[wpv-post-date]</div>

at diferent places but nothing worked well...

If you prefer I can provide access to the backend.

Thank you in advance!

#1480799

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Sure, let me check the backend, I'm sure you are very nearly there...

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site.

#1481085

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hmm. There seems to be something very odd going on with that View. I've been making a few changes trying to get it to work, but that conditional check seems to be breaking things. And as I type this I have a suspicion it may be related to this: https://toolset.com/errata/shortcodes-in-conditionally-displayed-content-may-not-be-executed-on-the-front-end/

In fact, that is almost certainly it. The second post which isn't displaying has a lot more image field values, and that means longer text within the conditional shortcode to evaluate.

I'm going to need a rethink, and I'll have to get back to you in the morning, now....

#1481087

Thank you!

#1483015

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, so remove the conditional test in the output of the View for whether the field is empty.

Instead, you will use the wpv_filter_query API hook to modify the query arguments, so that the View only returns queries with some value for the image field in the first place, then there is no need to test each post in the View results.

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You can add the following code at Toolset > Settings > Custom Code:

function tssupp_filter_query($view_args, $view_settings, $view_id)
{

    $target_views = array( 123 ); // List of View IDs to apply this to
    $field = 'galeria'; // Slug of field that must not be empty

    if (in_array($view_id, $target_views)) {

        $view_args['meta_query'] = array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-'.$field,
                'compare' => 'EXISTS'
            )
        );
    }

    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

You should just need to edit the ID of the View that this filter applies to.

Can you try that and confirm it works?

#1483033

Hi Nigel,

Thank you for your reply! I'm afraid it still doesn't filter as expected. Can you please check if I'm doing it right?

Here is my code:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

function tssupp_filter_query($view_args, $view_settings, $view_id)
{
 
    $target_views = array( 42239 ); // List of View IDs to apply this to - I typed the view ID - 42239
    $field = 'galeria'; // Slug of field that must not be empty
 
    if (in_array($view_id, $target_views)) {
 
        $view_args['meta_query'] = array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-'.$field,
                'compare' => 'EXISTS'
            )
        );
    }
 
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

Here is the template for the view:

<div class="col-3">
  <a href='[wpv-post-featured-image size="large" output="url"]' rel="group-[wpv-post-id]">
    [wpv-post-featured-image size="large"]
  </a>
  [wpv-for-each field='wpcf-galeria']
    <a href="[types field='galeria' output='raw'][/types]" rel="group-[wpv-post-id]">
    </a>
  [/wpv-for-each]
  <div class="multimedia-gallery-title">[wpv-post-title]</div>
  <div class="multimedia-gallery-date">[wpv-post-date]</div>
</div>

And here is the result:
hidden link

As you can see, still all the posts are displayed.

Thank you in advance!

#1483035

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Did you make sure that the code snippet is active?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.