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.
Now, a new menu named Brands appears. It’s my new Settings Page.
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 Group | Group | brand_group |
Brand Logo Upload | Single Image | brand_logo_upload |
Brand Logo Url | Url | brand_logo_url |
Brand Name | Text | brand_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.
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.
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.
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.
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.
Since we created custom fields for the Settings Page, we go to the Site tab then find the created custom fields.
Because this group is cloneable so whenever you open the group here to find any field, it will auto-generate a Loop.
Delete the text ‘INSERT SUB-FIELDS HERE” and 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.
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.
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.
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 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.
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.
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.
4. Styling the section
In the view, move to the CSS tab and add css into 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.
You may be interested in: