Each site usually has only one hero image. Then, the visitors will see the same image as well as the same tagline and information on the hero section. It may be boring and can not showcase more than one prominent campaign. Having multiple hero sets with different content and displaying them randomly will make your website more interesting. Let’s do it with the help of MB Views from Meta Box.

This is an example.

The hero sets display randomly with different backgrounds and content

Video Version

Before Getting Started

Each set of the hero section includes the image as the background, a tagline, and a short description. They are saved in custom fields in a settings page.

So, there are some tools we need to practice in this case:

We need the Meta Box core plugin to have a framework to create a settings page and custom fields for the hero section. It’s available on wordpress.org.

We also need some advanced features from Meta Box which is from some of its extensions:

  • MB Settings Page: to create a settings page to input hero image and other information;
  • MB Views: to get data from custom fields and display them on the homepage as the hero section;
  • Meta Box Builder: to have a UI on the back end to create custom fields easily.

You can install them individually or just use Meta Box AIO.

And, to build the page, you can use MB Views, Gutenberg, or any page builder as you want.

Let’s start!

Create a New Settings Page

We’ll use a settings page to include all the content of the hero section, including the images.

So, go to Meta Box > Settings Pages and create a new page.

Go to Meta Box > Settings Page and create a new one

After publishing, you can see a new settings page appear in the Admin Dashboard.

The created settings page displays in the menu of Admin Dashboard

We’ve just initialized the page, so it still is blank.

The settings page is blank now

In the next step, we’ll create some custom fields for this settings page.

Create Custom Fields

Go to Meta Box > Custom Fields to create a new field group.

Go to Meta Box > Custom Fields to create a new field group

Each set displayed on the hero section will be saved in a clonable group with some subfields inside. So, this is the structure of the fields I created.

Name Field Type Settings
Hero Image Group
  • Cloneable
  • Collapsible
      Image Single image
    Subfield
      Title Text
    Subfield
      Subtitle Textarea
    Subfield

The structure of the fields with a clonable group and subfields

Since we have multiple hero sets, I set the group as cloneable. This is the most pivotal setting in this practice.

Set the group is cloneable

We should set this group to be collapsible to see the content more tidily.

Set the group to be collapsible to see the content more tidily

You also can add the title for the group. The {#} is for numbering the sets.

Add the title for the group.

After having all the fields, move to the Settings tab, set the Location as Settings Page, and choose the created settings page to assign custom fields to it.

Move to the Settings tab, set the Location as Settings Page, and choose the created settings page to apply custom fields to it

Go to the created settings page, you can see the custom fields are ready.

The custom fields are ready on the settings page

Just add the hero image and other content.

Click on the Add more button to add another set of the hero image and content.

Click on the Add more button to add another set of the hero image and content

To get and display data, we’ll use MB Views in the next step.

Create a Template for the Hero Section

Go to Meta Box > Views and create a new template.

Go to Meta Box > Views and create a new view

Get All Data from Custom Fields

To get data from the custom fields, click on the Insert Field button, and look for any custom field we want from the list.

Click on the Insert Field button, and look for the custom field from the list to get the data

Since the fields in this case are on a settings page, go to the Site tab. You will see the group of fields that we created.

The group we created in the Site tab

Click on it and some lines of code will be generated in the Template section.

Some lines of code will be generated in the Template section when click on the group

In there:

  • {% set group = attribute( site, 'hero-image' ) %}: is to get data from the group field which on a settings page with the ID as hero-image;
  • {% for clone in group.hero_image %}: is the loop to get full of the data of the cloneable group that has ID as hero_image.

Next, just choose a subfield of the group from the list to insert them into the loop.

Choose a subfield of the group from the list to insert them into the loop as usual

Then, move to the settings of the view. There are some options to choose from. I prefer the default one to set this template in the kind of a shortcode to have the shortcode when publishing.

Set the view type as a shortcode to have the shortcode embedded in any place

After that, go to the homepage, and embed the shortcode to any place.

Embed the shortcode to any place in the homepage

All the images and content of all sets that I input to the fields are displayed.

All the images and content of all sets that I input to the fields are displayed

However, we need only one set display at once. So, we should customize the template a little bit more to choose randomly which data will be displayed.

Customize to Random Get a Set of the Hero Images

As you may know, normally, the data of a non-clonable group will be an array as follows:

[[image => "image"],[title => "title"],[description => "description"]]

And with a clonable group, the data will be an array like this:

[[[image => "image1"],[title => "title1"],[description => "description1"]],
[[image => "image2"],[title => "title2"],[description => "description2"]],
[[image => "image3"],[title => "title3"],[description => "description3"]]]

We need only one of the hero sets for each appearance. So, only one associative array will be obtained. Pay heed that each associative array here has its own order number. We’ll use it to choose which one will be displayed.

Now, I’ll replace the loop to the following line of code to create a variable:

{% set item = random(group.hero_image) %}

Replace the loop to another line of code to create a variable

Explanation:

  • random(group.hero_image): this is the function in PHP to randomly choose an item number of the group field. It returns the order number of an associative array;
  • hero_image: the ID of the group field.

Also, customize the code of the image to change the output of them.

Customize the code of the image to change the output of them

As well as replace the prefix with the name of the created variable. It helps to return only the image which is in the corresponding array.

Replace the prefix with the name of the created variable

Also change the prefix for the title and subtitle.

Change the prefix for the title and subtitle

After updating the views, go to the homepage and you will see that only one set of the hero image will be displayed at once.

Then, only one set of the hero image will be displayed at once

When we refresh the page, another set will be there instead.

When we refresh the page, another set will be there instead

There is a point here that I turned the image to its URL since I want it to be the background for this section in the next step.

Style the Hero Section

Go back to the view to edit the template once again.

Add some div tags and classes for the content, as well as turn the image to the background.

Add some div tags and classes for the content, as well as turn the image to the background

To style the section, move to the CSS tab, add some code.

To style the section, move to the CSS tab, add some code

Back to the homepage, there will be a new look of the hero section.

The new look of the hero section

Just refresh the page for several times, you will see the picture changed along with the different titles and subtitles.

The hero sets display randomly with different backgrounds and content

Last Words

Your visitor can be attracted to the random hero image with multiple messages and images. MB Views can help the process of displaying them more easily.

In the case that you want to show images as a slideshow, the article of image gallery and the series of displaying images from the clonable field can be useful for you.

If you have any questions or ideas, leave comments below, and don’t forget to keep track of our channel for more helpful tutorials. Thanks for reading!

  • How to Randomize Hero Image in WordPress - P1 - Using MB Views

Other case studies you might be interested in

  1. Create A Dynamic Landing Page in WordPress Using Custom Field
  2. Create a Filter to Find Hotels by Location
  3. Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
  4. Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
  5. Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
  6. Create Dynamic Favicon in WordPress using Meta Box plugin
  7. Create Posts Series in WordPress Using Meta Box
  8. Display The Latest Products Section - P2 - Using Meta Box and Elementor
  9. Display The Latest Products Section - P3 - Using Meta Box And Oxygen
  10. How to Add Custom Fields for WooCommerce - P2 - Using MB Views
  11. How to Add Custom Fields to Display Banners using Meta Box Plugin
  12. How to Add Guest Authors and Guest Posts - Using Meta Box
  13. How to Add Related Posts - Using Custom Fields
  14. How to Build a Hotel Booking Website Using Meta Box - P1
  15. How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
  16. How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
  17. How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
  18. How to Create a Classified Ads Website using Meta Box
  19. How to Create a Custom 404 Page in WordPress - P1 - Using Meta Box and Elementor
  20. How to create a FAQs page - P5 - Using Meta Box and Breakdance
  21. How to Create a Product Page - P2 - Using Meta Box and Oxygen
  22. How to Create a Product Page - P3 - Using Meta Box and Bricks
  23. How to Create a Product Page - P4 - Using Meta Box and Elementor
  24. How to Create a Product Page - P5 - Using Meta Box and Gutenberg
  25. How to Create a Product Page - P6 -Using Meta Box and Breakdance
  26. How to Create a Product Page - P7 - Using Meta Box + Kadence
  27. How to Create a Product Page - P8 - Using Meta Box and Brizy
  28. How to Create a Product Page - P9 - Using Meta Box and Divi
  29. How to Create a Product Page using Meta Box Plugin
  30. How to Create a Recipe - P2 - Using Meta Box and Oxygen
  31. How to Create a Recipe - P3 - Using Meta Box and Elementor
  32. How to Create a Recipe - P4 - Using Meta Box and Bricks
  33. How to Create a Recipe - P5 - Using Meta Box and Zion
  34. How to Create a Recipe - P6 - Using Meta Box and Brizy
  35. How to Create a Recipe - P7 - Using Meta Box and Breakdance
  36. How to Create a Recipe - P8 - Using Meta Box and Kadence
  37. How to Create a Recipe - P9 - Using Meta Box and Divi
  38. How to Create a Recipe with Meta Box Plugin
  39. How to Create a Simple Listing - P2 - Using Meta Box and Bricks
  40. How to Create a Simple Listing - P3 - Using Meta Box and Breakdance
  41. How to Create a Simple Listing - P4 - Using Meta Box and Elementor
  42. How to Create a Team Members Page - P1- Using Meta Box and Elementor
  43. How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
  44. How to Create a Team Members Page - P3 - Using Meta Box and Bricks
  45. How to Create a Team Members Page - P4 - Just Meta Box
  46. How to Create a Team Members Page - P6 - using Meta Box and Breakdance
  47. How to Create a Team Members Page - P7 - Using Meta Box and Kadence
  48. How to Create a Video Gallery Page - P2 - Using Meta Box + Bricks
  49. How to Create a Video Gallery Page - P3 - Using Meta Box and Breakdance
  50. How to Create a Video Gallery Page - P4 - Using Meta Box + Elementor
  51. How to Create a Video Gallery Page - P5 - Using MB Views
  52. How to Create a Video Gallery Page - P6 - Using Meta Box and Zion
  53. How to Create a Video Gallery Page Using Meta Box + Oxygen
  54. How to Create ACF Flexible Content Field with Meta Box
  55. How to Create an Auto-Updated Cheat Sheet in WordPress
  56. How to Create an FAQs Page - P1 - Using Meta Box and Elementor
  57. How to create an FAQs page - P2 - Using Meta Box and Oxygen
  58. How to create an FAQs page - P4 - Using Meta Box and Bricks
  59. How to Create an FAQs Page - P6 - Using MB Views
  60. How to Create an FAQs Page - P7 - Using Meta Box and Divi
  61. How to Create an FAQs Page - P8 - Using Meta Box and Kadence
  62. How to Create an FAQs Page - P9 - Using MB Blocks
  63. How to Create an FAQs Page -P3- Using Meta Box
  64. How to Create Buttons with Dynamic Link using Custom Fields
  65. How to Create Category Thumbnails & Featured Images Using Custom Fields
  66. How to Create Download and Preview Buttons - P1 - Using Meta Box and Bricks
  67. How to Create Download and Preview Buttons - P2 - Using Meta Box and Oxygen
  68. How to Create Download and Preview Buttons - P3 - Using MB Views
  69. How to Create Download Buttons in WordPress - Using Custom Fields
  70. How to Create Dynamic Landing Page in WordPress - P1 - Using Meta Box and Elementor
  71. How to Create Dynamic Landing Page in WordPress - P2 - Using Meta Box and Bricks
  72. How to Create Menus for Restaurants - P1 - Using Meta Box and Elementor
  73. How to Create Menus for Restaurants - P2- Using Meta Box and Bricks
  74. How to Create Notification Using Custom HTML Field
  75. How to Create Online Admission Form for School or University
  76. How to Create Online Reservation Form for Restaurants using Meta Box
  77. How to Create Relationships - P1 - Using Meta Box and Oxygen
  78. How to Create Relationships - P2 - Using Meta Box and Bricks
  79. How to Create Relationships - P3 - Using MB Views
  80. How to Create Relationships - P4 - Using Meta Box and Breakdance
  81. How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
  82. How to Create Taxonomy Thumbnails & Featured Images - P3 - Using Meta Box and Bricks
  83. How to Create Taxonomy Thumbnails & Featured Images - P4 - Using MB Views
  84. How to Create YouTube Video Timestamps on WordPress Website - P1 - Using MB Views
  85. How To Display All Listings On A Map With Meta Box
  86. How to Display Author Bio in WordPress - P1 - Using Meta Box and Bricks
  87. How to Display Author Bio in WordPress - P2 - Using MB Views
  88. How to Display Dynamic Banners in WordPress - P3 - Using MB Views
  89. How to Display Images from Cloneable Fields - P1 - with Gutenberg
  90. How to Display Images from Cloneable Fields - P2 - Using Meta Box and Oxygen
  91. How to Display Images from Cloneable Fields - P3 - with Elementor
  92. How to Display Images from Cloneable Fields - P4 - with Bricks
  93. How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
  94. How to Display Opening Hours for Restaurants - P2 - Using Meta Box and Oxygen
  95. How to Display Product Variations - P1 - Using Meta Box and Gutenberg
  96. How to Display Product Variations - P2 - Using Meta Box and Oxygen
  97. How to Display Product Variations - P3 - Using Meta Box and Bricks
  98. How to Display the Dynamic Banners - P2 - Using Meta Box and Bricks
  99. How to Display The Latest Products - P5 - Using Meta Box and Bricks

Leave a Reply

Your email address will not be published. Required fields are marked *