Skip to main content

Displaying images from cloneable fields - MB Views

Images like a series of brand logos of typical partners or avatar of clients in the testimonial section, could be saved in custom fields. This tutorial is on how to display them without using any page builders.

Video version

Preparation

We’ll need Meta Box and some of its extensions for this practice:

  • MB Settings Page: create settings pages for uploading images;
  • Meta Box Builder: it provides a UI to create custom fields;
  • Meta Box Group: organize custom fields into cloneable groups, where we input images;
  • MB Views: it helps you to create templates as well as get and display fields’ values.

1. Creating a settings page

Go to Meta Box > Settings Page > Add New and change the option name.

Create a Settings Page

Now, a new menu named Brands appears. It’s my new Settings Page.

The new Settings Page named Brands appears.

2. Creating custom fields

In the Admin Dashboard, go to Meta Box > Custom Fields > Add New. I created fields with the below structure.

Field

Types of Field

ID

Brand GroupGroupbrand_group
Brand Logo UploadSingle Imagebrand_logo_upload
Brand Logo UrlUrlbrand_logo_url
Brand NameTextbrand_name

This is a group with three subfields inside. This group is set to be cloneable to have more spaces to add different brands information.

Set this group as cloneable to have more spaces to add different brands information.

As you can see in the structure, I added 2 fields which are Single Image and Url. 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.

2 fields which are Single Image and Url help you upload images and input a link of image.

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

In the Settings tab, choose the Location as the Settings Page that we’ve just created to apply the custom fields.

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

The created custom fields are displayed.

3. Displaying data from fields

To get the input data from those custom fields to display the brand information, I will use MB Views to create a template. Go to Meta Box > Views and add a new view.

In the Template tab of the view, click the Insert Field button to choose the value from which fields to display.

In the Template tab, you can insert fields to get their data.

Since we created custom fields for the Settings Page, we go to the Site tab then find the created custom fields.

In Site tab, find the custom fields that I created for the Settings Page

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

A Loop is auto generated whenever you open the group to find any field

Delete the text ‘INSERT SUB-FIELDS HERE” and choose a field from the list in the sidebar to add a field inside the loop.

choose a field from the list in the sidebar to add a field inside the loop

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

Get the value from the Single Image field when the URL field is empty

When inserting the fields, I also edit the code a little bit to create a rule for displaying images. For example, I set a rule that if there is no URL input to the URL field, the value from the Single Image field will be obtained and displayed.

Get the value from the Single Image field

The following code will stipulate that if there is no input data in the Brand Logo Upload field, the image from the link in the URL field will be displayed.

The following code will stipulate that if there is no input data

Next, if there is data in both the Image Upload and URL field, I set priority to display the image of the Brand Logo Upload field.

If there is data set priority to display image

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

Remember to insert the added information in the text line

If you want to style this section, add some div 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 Settings section, choose the type of view as Shortcode.

choose the type of view as shortcode

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. But this section hasn’t looked good yet. Thus, we’ll need some JS and CSS to make it look better.

All the images appeared

4. Styling the section

In the view, move to the CSS tab and add css into the box.

Add Css code to the box

All the css code is uploaded to Github, you can refer to it.

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

The result after adding css code


You may be interested in: