Displaying dynamic banners in multiple places on a website with the same content but varying in size and position may not be easy to design. If you are looking at how to create banners without using any page builder, we will share an easy and straightforward tip using MB Views that you can follow. Let’s dive in to explore the process of creating banners in detail.

This is an example for the banners that we will create in this practice.

This is an example for the banners

Video Version

Before Getting Started

I’ll create two banners with the same content but varying in size and position. They are not rendered as images. The background, text, color, or any element of the banner are stipulated from the content saved in custom fields. So you can modify the banner context to whatever you want without having to update the layout or develop a new one.

Since we use the custom fields to display some of the content banners, we need the Meta Box plugin for the framework to do that. You can download it directly from wordpress.org.

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

  • MB Settings Page: to create a settings page to input banner information;
  • MB Views: to create a template for the banner. It’s just optional;
  • Meta Box Builder: to have a UI on the back end to create custom fields easily.

You can install these extensions individually or just use Meta Box AIO.

Create a Settings Page

We’ll use a settings page to include all the content, and other settings of the banner.

Go to Meta Box to create a settings page.

Go to Meta Box to create a settings page

There is no need for special settings for it in this practice.

After publishing, you can see a new settings page named Banner appeared.

A new settings page named as Banner

It’s blank now, so just move on to create custom fields for this page.

The settings page for the banner is blank page so far

Create Custom Fields

Each element for the content of the banner should be put in a separate custom field. These are some fields that I’ll create for the banner.

There are some fields for banners

The first field is to set to display the banner on the frontend or not. You can turn on or turn off the banner easily thanks to it.

This field is to set to display the banner on the frontend or not

All the other fields are the content of the banner. They’re just typical ones. You should create fields based on your own requirements for the banner.

Now, move to the Meta Box menu to create fields.

Choose the Checkbox type for the first field, which regulates whether to display the banner or not.

This is the Checkbox type to display the banner or not

For the other fields for the content of the banner, just create them without special settings.

After creating all the fields, move to the Settings tab, choose Location as Settings Page, and select Banner to display the created fields to the settings page we use for the banner.

Move to the Settings tab, choose Location as Settings Page, and select Banner to display the created fields

Now the custom fields are ready on the settings page.

The custom fields on the settings page

Just add content for the banner.

Create a Template for Banner

We’ll create a template and generate a shortcode to easily display the banner anywhere.

There are two ways to do this.

  • Adding PHP code into the theme’s file. If you change the theme, the template as well as the shortcode will be missed.
  • Using the MB Views extension from Meta Box. It will ensure it always works.

We’ll go through to see both of these ways, then you can experience them all.

Create a Template for Banner by Adding PHP code to the Theme’s File

Get Data for Banner

Go to the functions.php file, and add some lines of code.

Go to the functions.php file, and add some lines of code

In there:

$settings = get_option( 'banner' );

This line is to get data from the page. In there, 'banner' is the option name of the settings page.

All of these following lines are to get data from the custom fields.

These lines are to get data from the custom fields

wp_get_attachment_image_src( )

This function is to get the URL of the image. 'image' is the ID of the field.

$show = $settings['show'];

This line is to get the value saved in this field since we have a checkbox field choosing to show or hide the banner.

The rwmb_meta( ) function is to get the data in text from fields.

This function to get the data in text from fields

These are the ID of the fields.

These are the ID of the fields

Beside that, ['object_type' => 'setting'] stipulates the object type as a settings page.

These lines is to display the banner by using all the obtained data.

These lines is to display the banner by using all the obtained data

This if ( $show == 1 ) { } function is the condition to check that you check the box to allow showing the banner or not. If yes, the value saved in the field will be 1.

This is the banner that we will display.

This is the banner will be displayed

I do not display all the obtained data directly. Some of them will be used as HTML attributes to specify how the image or the text will be.

For example, the URL of the image used for the background by this: background-image: url(' . $image_attributes[0] . ' ).. The color and position also are used in the same way.

Display the Banner

In the above code, I also created a shortcode for the banner, then we can embed it to any place.

add_shortcode( 'banner-shortcode', 'short_code_banner' );

banner-shortcode is the name of the shortcode.

Go to the editor of any page, just paste the shortcode.

For instance, I add the shortcode to a page content and will display it as full width later.

Go to the editor of any page, just paste the shortcode

Then you’ll see the banner displayed.

The banner displayed without styling

To style the banner, we should add some CSS.

Add some CSS for styling

Here is the new look of the banner.

The new look of the banner

I also will add the banner to the right sidebar. Also use the shortcode.

Add shortcode to the right sidebar

This is how the banner displays.

This is how the banner displays

You can see that the banners in both places are the same in the content and layout, but different in the size.

With dynamic banners, you can easily change the banner content and layout without spending time designing a new one, as well as place the banner in different areas and make changes of them at the same time.

Create a Template for Banner Using MB Views

Create Template and Get Data

Go to Views to create a new template.

In the Template tab, instead of adding code, we will insert fields one by one that we want to get data from.

In the Template tab, insert fields one by one to get data from

Since our fields are on a settings page, in the Site section, you will see the list of the fields that we use for the banner.

The list of the fields that we use for the banner

Just click one by one to insert fields to the template.

Click one by one to insert fields to the template

After publishing this template, it will automatically generate a shortcode. We’ll use it to display the banner later.

A shortcode will be automatically generated

Display the Banner Content

The same with the method one when we have a shortcode by adding code to the theme file, now we’ll copy the shortcode from the view, then go to a page editor, paste the shortcode to any place.

Go to a page editor, paste the shortcode to any place

On the frontend, the data will display in the form like this.

On the frontend, the data will display in the form

Beautify the Banner Display

Go back to the template to customize it a bit more to regulate how the data should be rendered on the page.

The logic of the code is the same with method 1 with PHP, but different in detailed text.

Move some lines of code to make it seem shorter and simpler

{% if(site.banner.show) == 1 %}

This line is a condition for showing or hiding the banner. It means that if the field with this ID is checked, the stored value will be 1, and then the banner will be allowed to display.

style="width: {{ site.banner.width }}

This line helps turn data from custom fields to be an attribute to stipulate the style of the banner. In this case, it’s the width of the image {{ site.banner.width }} means the field has the ID as width in the settings page has the ID as banner.

Just update the template, and see the new look of the banner on the page.

The new look of the banner on the page

To prettify the banner, go back to the template in the view, add some CSS.

For styling, go back to the template, add some CSS

Now, the banner displays on the frontend at full width with the right layout.

The banner displays on the frontend at full width with the right layout

Display Banners on Multiple Positions

We have a shortcode of the template for the banner like this.

This is a shortcode of the template for the banner

Just add it to multiple places on a page, or even different pages.

Add shortcode to multiple places on a page

Then you will have the banner in different places.

The final result of the banner with the same content but different sizes

Last Words

So, no matter what method you choose, you will get the same results with the ability to add banners in multiple places, have the same content for all the banners but different sizes, and dynamic content that you can change easily without affecting the banner layout.

In the case that you want to use any page builder, you can follow how to create a dynamic banner with page builder. Just take a look and follow the guides.

Let’s try and enjoy it. If you want to suggest any tutorials, feel free to leave a comment and keep track of our blog. Thanks for reading!

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 *