How to Display Images from Cloneable Fields - P1 - with Gutenberg
Tags: metabox, gutenberg, tutorials, images
Published: 2022-02-22 02:30:38

Display images from cloneable fields</strong> is a common task for those who are building a website. For example, you may want to <strong>display a series of brand logos</strong> of typical clients or partners, even a testimonial section with an avatar along with the clients’ name and quote. In this series, we will take a ride to do it with a simple case study, brand logos.

<!--more-->

<img class="aligncenter" src="https://i.imgur.com/4RCrG0j.gif" alt="The example of displaying images from cloneable fields using Meta Box + Gutenberg" width="1200" height="675" />

If you are using Gutenberg, this part is for you. For other page builders, please wait for the upcoming tutorials.
<h2>Before Getting Started</h2>
We’ll need <a href="https://wordpress.org/plugins/meta-box/">Meta Box</a> and some of its extensions for this practice:
<ul>
 	<li><a href="https://metabox.io/plugins/mb-settings-page/">MB Settings Page</a>: it helps you to create settings pages, we’ll need a settings page to place the custom fields into;</li>
 	<li><a href="https://metabox.io/plugins/meta-box-builder/">Meta Box Builder</a>: it provides a UI to create custom fields easily;</li>
 	<li><a href="https://metabox.io/plugins/meta-box-group/">Meta Box Group</a>: this extension helps to organize custom fields into cloneable groups, where we input images today;</li>
 	<li><a href="https://metabox.io/plugins/mb-views/">MB Views</a>: it helps you to create templates as well as get and display fields’ values without touching the theme’s files.</li>
</ul>
You can install these extensions individually or use Meta Box AIO.
<h2>Video Version</h2>
https://www.youtube.com/watch?v=nteLH6DDKSI
<h2>Step 1: Create a Settings Page</h2>
Normally, we’ll create a Custom Post Type to create multiple posts for brands then each post is for a brand. But, today, I do a more convenient and simple way - creating a <strong>Settings Page</strong>. The information about all the brands will be input to that page. It means that all of them will be in one place only.

Now, go to <strong>Meta Box</strong> &gt; <strong>Settings Page</strong> &gt; <strong>Add New</strong>. In this case, the <strong>Settings Page</strong> is quite simple to contain the image of each brand, so just change the option name.

<img class="aligncenter" src="https://i.imgur.com/vvcsa9J.png" alt="Create a Settings Page" width="1200" height="681" />

Now, a new menu named <strong>Brands</strong> appears. It’s my new <strong>Settings Page</strong>.

<img class="aligncenter" src="https://i.imgur.com/p8HPpzh.png" alt="The new Settings Page named Brands appears." width="1200" height="682" />
<h2>Step 2: Create Custom Fields for the Settings Page</h2>
In the Admin Dashboard, go to <strong>Meta Box</strong> &gt; <strong>Custom Fields</strong> &gt; <strong>Add New</strong>. I created fields with the below structure.
<table>
<thead>
<tr>
<th>
<p style="text-align: left;"><b>Field</b></p>
</th>
<th>
<p style="text-align: left;"><b>Types of Field</b></p>
</th>
<th>
<p style="text-align: left;"><b>ID</b></p>
</th>
</tr>
</thead>
<tbody>
<tr>
<td><span style="font-weight: 400;">Brand Group</span></td>
<td><span style="font-weight: 400;">Group</span></td>
<td><span style="font-weight: 400;">brand_group</span></td>
</tr>
<tr>
<td><span style="font-weight: 400;">          Brand Logo Upload</span></td>
<td><span style="font-weight: 400;">Single Image</span></td>
<td><span style="font-weight: 400;">brand_logo_upload</span></td>
</tr>
<tr>
<td><span style="font-weight: 400;">          Brand Logo Url</span></td>
<td><span style="font-weight: 400;">Url</span></td>
<td><span style="font-weight: 400;">brand_logo_url</span></td>
</tr>
<tr>
<td><span style="font-weight: 400;">          Brand Name</span></td>
<td><span style="font-weight: 400;">Text</span></td>
<td><span style="font-weight: 400;">brand_name</span></td>
</tr>
</tbody>
</table>
This is a group with three sub-fields inside. This group is set to be cloneable to have more spaces to add different brands information.

<img class="aligncenter" src="https://i.imgur.com/WLPGJG8.png" alt="Set this group as cloneable to have more spaces to add different brands information." width="1200" height="679" />

As you can see in the structure, I added 2 fields which are <strong>Single Image</strong> and <strong>Url</strong>. One allows you to upload images and save them in the library, and one allows you to input a link of an image from anywhere.

<img class="aligncenter" src="https://i.imgur.com/7PffxDP.png" alt="2 fields which are Single Image and Url help you upload images and input a link of image." width="1200" height="682" />

After having all the needed fields, go to the <strong>Settings</strong> tab and choose the <strong>Location</strong> as the <strong>Settings Page</strong> that we’ve just created to apply the custom fields to it.

<img class="aligncenter" src="https://i.imgur.com/qqZo8FM.png" alt="In the Settings tab, choose the Location as the Settings Page that we’ve just created to apply the custom fields." width="1200" height="682" />

Back to the <strong>Settings Page</strong>, you’ll see all of the created custom fields. You can click <strong>Add more</strong> to add another brand’s information.

<img class="aligncenter" src="https://i.imgur.com/5KSKj8o.gif" alt="The created custom fields are displayed." width="1200" height="675" />

Now, let’s fill the information into the fields and move to the next step.
<h2>Step 3: Display Brands Information</h2>
To display the brand information, you normally must go to the theme’s files to add code or use a page builder. However, you have another way with Meta Box. We have the <strong>MB Views</strong> extension to create templates without touching the theme’s files.

<img class="aligncenter" src="https://i.imgur.com/ha1oMWC.png" alt="In the Template tab, you can insert fields to get their data." width="1200" height="679" />

Instead of typing code into the box in the <strong>Template</strong> tab, you can insert fields to get their data. And, in the previous tutorial, we used to create custom fields for posts, so we choose one from the <strong>Post</strong> tab. But, in this practice, we created custom fields for the <strong>Settings Page</strong>, so we go to the <strong>Site</strong> tab then find the custom fields that I created.

<img class="aligncenter" src="https://i.imgur.com/0McGXgF.png" alt="In Site tab, find the custom fields that I created for the Settings Page. " width="1200" height="682" />

Because this group is cloneable so whenever you open the group here to find any field, it will auto-generate a Loop.

<img class="aligncenter" src="https://i.imgur.com/Db4vgBV.png" alt="A Loop is auto generated whenever you open the group to find any field." width="1902" height="1066" />

When inserting the fields, I also edit the code a little bit to create a rule for displaying images.

<img class="aligncenter" src="https://i.imgur.com/Y2M20CR.png" alt="Edit the code a little bit to create a rule for displaying images." width="1200" height="672" />

The first part is to set a rule that if there is no URL input to the <strong>URL</strong> field, the value from the <strong>Single Image</strong> field will be obtained and displayed.

<img class="aligncenter" src="https://i.imgur.com/5b9Cpoe.png" alt="Get the value from the Single Image field when the URL field is empty." width="1200" height="678" />

When you insert the logo from the <strong>Single Image</strong> field, you can choose to size to output. Here I set it to be the thumbnail.

<img class="aligncenter" src="https://i.imgur.com/QTpPaJI.png" alt="Set logo size as thumbnail." width="1200" height="681" />

In the event that there is no input data in the <strong>Brand Logo Upload</strong> field, the image from the link in the <strong>URL</strong> field will be displayed.

<img class="aligncenter" src="https://i.imgur.com/Khoj7SP.png" alt="Get the value from the URL field when the Single Image field is empty." width="1200" height="676" />

If there is data in both the <strong>Image Upload</strong> and <strong>URL</strong> field, I set priority to display the image of the <strong>Brand Logo Upload</strong> field.

<img class="aligncenter" src="https://i.imgur.com/YYw4oTg.png" alt="We display the image of the Brand Logo Upload field if both the Image Upload and URL field have the value." width="1200" height="681" />

If you have any further information for the brand, such as the <strong>Brand Name</strong> I have in this case, remember to insert them in the next lines.

<img class="aligncenter" src="https://i.imgur.com/Lqatwen.png" alt="Get the value of Brand Name field." width="1200" height="681" />

If you want to style this section, add some <strong>div</strong> tags. I also did it and here is all the code.

```
<div class="brand-group">
{% for clone in site.brands.brand_group %}
	<div class="brand-img">
	{% if clone.brand_logo_url|trim is empty %}
		<img src="{{ clone.brand_logo_upload.thumbnail.url }}" width="{{ clone.brand_logo_upload.thumbnail.width }}" height="{{ clone.brand_logo_upload.thumbnail.height }}" alt="{{ clone.brand_logo_upload.thumbnail.alt }}">
	{% elseif clone.brand_logo_upload.thumbnail.ID|trim is empty %}
		<img src="{{ clone.brand_logo_url }}">
	{% else %}
		<img src="{{ clone.brand_logo_upload.thumbnail.url }}" width="{{ clone.brand_logo_upload.thumbnail.width }}" height="{{ clone.brand_logo_upload.thumbnail.height }}" alt="{{ clone.brand_logo_upload.thumbnail.alt }}">
	{% endif %}
	<p class="name">
		{{ clone.brand_name }}
	</p>
	</div>
{% endfor %}
</div>

```
To finish, move to the <strong>Settings</strong> section, choose the type of view as <strong>Shortcode</strong>.

<img class="aligncenter" src="https://i.imgur.com/Ld032oe.png" alt="Choose the type of view as Shortcode in Settings section." width="1200" height="681" />

After publishing this view, a shortcode will be generated automatically. You can copy and paste it to any page you want to display the template.

Now, you can see all of the brand logos along with their names displayed already.

<img class="aligncenter" src="https://i.imgur.com/D47dXJV.gif" alt="After inserting fields, the brand logos along with their names displayed already, but it hasn’t looked good yet." width="1200" height="675" />

You see that this section hasn’t looked good yet. Thus, we’ll need some JS and CSS to make it look better.
<h2>Step 4: Create Slider for the Brand Logos</h2>
As you see in the first image I put at the beginning of this post, the logos are in a slider, and brand names are displayed correspondingly. To have it, I’ll use some JS and CSS.

However, I’m using the <strong>My Custom Functionality</strong> plugin instead of adding them directly to the theme. Thanks to that, the slider won’t be affected when I change the theme. You can download this plugin from <a href="https://github.com/srikat/my-custom-functionality">GitHub</a> and install it on your website.
<h3>Download the JS and CSS Library</h3>
For the JS and CSS, I use the Slick library. It’s also available on <a href="https://github.com/kenwheeler/slick">Github</a>. It includes several files, but we just need three of them.

<img class="aligncenter" src="https://i.imgur.com/f8GR8Et.png" alt="JS and CSS files in the Slick library." width="1200" height="604" />

Go to the folder of the <strong>My Custom Functionality</strong> plugin. Upload them into the corresponding JS and CSS folders.

<img class="aligncenter" src="https://i.imgur.com/xJam2P5.png" alt="Upload the slick-theme.css file into the css folder of My Custom Functionality plugin." width="1200" height="512" />
<h3>Create Custom JS for Slider and Rules</h3>
Create a <code>custom.js</code> file in the <code>JS</code> folder, and add code.

<img class="aligncenter" src="https://i.imgur.com/GBQ1fSX.png" alt="Add code to custom.js file." width="1200" height="974" />

```
jQuery(document).ready(function ($) {
    jQuery(".brand-group").slick({
        infinite: false,
        dots: false,
        autoplay: false,
        speed: 800,
        autoplaySpeed: 4000,
        slidesToShow: 7,
        slidesToScroll: 7,
        nextArrow: '<button class="next-btn">Next</button>',
        prevArrow: '<button class="prev-btn">Previous</button>'
    });
})

```
<code>jQuery(".brand-group").slick({ })</code> is used to create a slider for the elements which have <code>.brand-group</code> class.
<h3>Declare the JS and CSS Files</h3>
We need to declare all the JS and CSS files that we have just uploaded and created. I add this code inside the <code>custom_enqueue_files()</code> function in the <code>plugin.php</code> file.

```
wp_enqueue_style('slick', plugin_dir_url( __FILE__ ).'/assets/css/slick.css');
wp_enqueue_style('slick-theme', plugin_dir_url( __FILE__ ).'/assets/css/slick-theme.css');

wp_enqueue_script('custom', plugin_dir_url( __FILE__ ).'/assets/js/custom.js', ['jquery']);
wp_enqueue_script('slick-min', plugin_dir_url( __FILE__ ).'/assets/js/slick.min.js', ['jquery']);

```
<img class="aligncenter" src="https://i.imgur.com/JGL6YUf.png" alt="Add code in the plugin.php file" width="1200" height="975" />

Now, the brand images have already turned into a slider.

<img class="aligncenter" src="https://i.imgur.com/CVUTK2x.gif" alt="The brand images have already turned into a slider." width="1200" height="675" />

We still need to add some CSS to make it look better.
<h2>Step 5: Style the Slider using CSS</h2>
Traditionally, you have to add CSS to the Customizer or the theme’s files. But, with <strong>MB Views</strong>, you have a <strong>CSS</strong> tab for it.

<img class="aligncenter" src="https://i.imgur.com/kTLX6IG.png" alt="Add code to CSS tab." width="1200" height="681" />

All the CSS code is uploaded to <a href="https://github.com/wpmetabox/tutorials/blob/effb1b8d4729da57bf3fe5ee77dc2fe2827678a7/display-images-from-cloneable-fields-with-Gutenberg/custom.css">Github</a>, you can refer to it.

After adding CSS, my brand section turned into a new look.

<img class="aligncenter" src="https://i.imgur.com/4RCrG0j.gif" alt="After adding CSS, the brand logos section turned into a new look." width="1200" height="675" />
<h2>Last Words</h2>
You see that <strong>displaying images from cloneable fields using Meta Box</strong> is not a difficult task. In my opinion, it’s very practical in some cases. We hope that this tutorial will help you to do it easier.

Remember to keep track of our channel for more upcoming tuts and feel free to leave a comment if you have any ideas or concerns.