Skip to main content

Creating buttons with dynamic links

Dynamic links are created to show various links in different posts and are displayed as buttons. It can be made in a variety of ways. One method is to use custom fields.

For instance, I have a website about theme reviews here. Each post about a theme has buttons for its own demo and download pages. They are dynamic links.

Create buttons with dynamic links

Video version

Preparation

In addition to using Meta Box, make sure you already have these extensions:

  • Meta Box Builder: It provides UI to create custom fields;
  • MB Views: Build front-end templates for WordPress without touching theme files. Support Twig and all field types.

If you are using a page builder such as Elementor or Beaver Builder, please activate the corresponding integration from Meta Box.

All these extensions are available in the Meta Box AIO, which includes the Developer and Lifetime licenses.

Creating custom fields for URLs

First, we need custom fields for saving URLs for each post. If you’ve had Meta Box Builder in your pocket, you will have a UI on the backend to create custom fields visually. Otherwise, please go to the Online Generator, create fields, generate code, and copy it to get the fields.

They are quite simple with a type of URL and there are no special settings.

Choose URL type of custom field" height

After having these fields, please go to the posts and input some links. That way, you will have data to check if the buttons work.

Creating buttons without page builder

For someone who is not using any page builder on their site, this part may help. MB Views from Meta Box will help to create a button easily with a shortcode.

Just go to Views and create a new view with this code:


<a class="wp-block-button__link" href="{{ post.view_demo }}">
View demo
</a>
<a class="wp-block-button__link" href="{{ post.download }}">
Download
</a>

In there, view_demo and download are custom fields with IDs. By the way, because I’m using the eStar theme, I must use the wp-block-button__link class to create buttons.

The view will generate a shortcode as you see in the below image. Just copy and embed it somewhere.

The view will generate a shortcode

For instance, I pasted it to a sidebar on the single blog post page and got these default buttons.

Example of the default buttons

Note that this look of the button is from the theme default.

I’ve already pre-made a template using Oxygen. It is applied to blog posts.

I will set a container to contain two buttons right under the featured image of the post.

Set a container to contain two buttons right under the featured image

Now, add a button. At the end of the URL section, there is a button linked to dynamic data. Click it, then choose Meta / Custom Field in the Post section. Next, scroll your mouse until you find the ID of the wanted field to connect to the button.

Set the wanted buttons

The remaining thing to do is style the button as you want.

Style the buttons as you want

I also created another one with a new style and linked it to another field. And, here they are in a post, with links.

Created another one with a new style and link it to another field

I also created a similar template with Beaver Builder.

First, I add columns to set the layout, and then I add a button. There’s a Link section for inserting a static link to the button’s settings. Click the plus icon to receive Meta Box URLs.

Create buttons with dynamic links using Beaver Builder

You will see the Meta Box Field option in the drop-down list. Choose it by clicking Connect.

Connect it with Meta Box field option

Then, choose the name of the field you want.

The last thing, style your button. That’s all.

To have another button, you can duplicate this one to save time, then reconnect it to another field as well as restyle it.

To restyle the template, reconnect it to another field

Now, go to a post. You will see the buttons with links.

Buttons with links

The last page builder is Bricks. In the same way, add a container with 4 columns first, then buttons.

Different from other page builders, Bricks allows you to choose the type of link from a dropdown list. To get data from custom fields in Meta Box, please choose Dynamic Data.

Meta Box is suggested with the name of custom fields

After that, you will see Meta Box is suggested with the name of custom fields following. Choose one if you want.

Meta Box is suggested with the name of custom fields

Bricks will render a corresponding link to this preview post so that you can check if the data is correct.

Bricks will render a corresponding link to this preview post so that you can check if the data is correct

After that, all the work is styling.

Style as you want" height

Then, I’ve got an ace button in the preview. Let’s duplicate it to have a new one. I’ll change the data source and restyle this new button.

Duplicate it to have a new one

Finally, here they are on a post.

The created post after all steps