Add Second Button to Blog Module Posts

Written by Dan Mossop

The Divi theme comes with a blog module, which lets you display a list of posts anywhere on your site, complete with featured image, excerpt and read more button. If you'd like to add a second button beside posts in the Divi blog module, here's a way to do it.

I've had a bit of a play around and come up with a basic implementation (in PHP). In this example I'm adding a "Buy Tickets" button with a URL taken from a custom field.

First, here's the code:

add_filter('et_pb_blog_shortcode_output', 'dbc_add_second_buttons_to_blog_module', 10);

function dbc_add_second_buttons_to_blog_module($content) {
	if (is_string($content)) {
		return preg_replace_callback('/<article.*?<\/article>/s', 'dbc_add_second_button_to_article', $content);
	}
	return $content;
}
function dbc_add_second_button_to_article($match) {
    if (isset($match[0])) { 
        $html = $match[0];
        $article = false;
        preg_match('/<article id="post-(\d*)"/', $html, $article);
        if (isset($article[1])) {
            $id = intval($article[1]);
            if ($id) {  
                $url = get_post_meta($id, 'buy_tickets_url', true); // Fetch url from post's custom field
                if (!empty($url)) {
                    $button = '<a href="'.esc_attr($url).'" class="more-link">Buy Tickets</a>';
                    $html = preg_replace('/(<a[^>]+class="more-link">)/s', $button.'\\1', $html);
                }
            }
        }
    }
    return $html;
}
You could add this, for example, into the functions.php file of a child theme, or using a plugin such as code snippets.

It looks a bit complicated, but don't let that put you off – what it's doing is reasonably straightforward. it modifies the HTML generated by the by the blog module, and then finds each article element in that HTML (corresponding to each blog post shown by the module).

For each article, it grabs the post id from the article's id attribute. It then tries to retrieve the ticket URL from a custom field called 'buy_tickets_url' from the post (I'll explain this in a bit). If it finds a ticket URL, it adds a "Buy Tickets" link with that URL alongside (actually, before) the read more link. Then the updated HTML gets returned to be displayed by the blog module.

The main thing you might need to change is where the ticket URL is retrieved from. Currently, the code is set up to use standard WP post custom fields. To test this, enable custom fields on a post by going into the backend edit screen for that post, and clicking on the options menu:
Then go to Preferences and enable Custom Fields, like so:
After saving the post, you should see an extra button added to the post in the blog module. Here's an example with the blog module in grid mode and some very basic styling:
Buttons can be added to other posts by adding a 'buy_tickets_url' custom field to them in the same way.

A couple of notes:

  • The extra button will be added to any blog module in which the post appears.
  • The code adds the "more-link" class to the new button, so that it will adopt the same styles as the standard "read more" link.

Run PHP Code Directly in your Divi Layouts

Unlock endless customization, automation, and dynamic functionality by seamlessly adding PHP code to your Divi pages and posts with the Divi PHP Code Module. Style, preview, and debug your PHP creations directly in the visual builder with robust error handling and enhanced security.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

6 Comments

  1. Hi, thanks for your effort and sharing it with us.
    What I am trying to do is to add a button from https://favoriteposts.com/ to the Blog Module, but just can't find a way. Thought your post would help me, but I just quite do not understand the function that should be used – with the lengthy string of letters. I'd appreciate your help. What I see in the place where you mentioned the code is a shortcode: et_pb_dmb_code_snippet with attribute code, which is: starts with YWRkX2ZpbHRlcignZXRfcG and goes on for ever.
    I'd appreciate your response.

    Reply
    • Hi Tomek, my apologies for the slow response. There was a temporary issue with the setup of one of the performance plugins I use that was preventing the code from displaying correctly. The correct code should now be visible in the post, should you wish to try again. Please let me know if you have any questions about it, etc. Thanks!

      Reply
  2. this code makes a problem – visual builder can't load

    Reply
    • Hi elsa, thanks for letting me know. I think I've identified the cause of the problem and I've updated to the code in the post to address the issue. If you replace the existing code on your site with the revised version from the post, it should resolve the Visual Builder loading issue. Please try this out and let me know if you encounter any further problems. Thanks!

      Reply
      • Sometimes the /wp-admin wont load if this function is in the function.php
        if i take the code out the website is instantly loading normal again

        Reply
        • Hey Chris, that's odd. It doesn't happen on my test site and I'm not sure how it is affecting your /wp-admin. One possibility is a typo when copying the code (e.g. a missing "}" at the end). If the code looks like it's been copied correctly, are you able to enable the WordPress debug log as described in this post:

          https://wp-staging.com/docs/enable-wordpress-debug-log-mode/

          After enabling logging, trigger the error and then check the debug.log file for any error messages. If you can send them through to me I should be able to get a clearer idea of what's going on and how to fix it. Thanks!

          Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

We may earn a commission when you visit links on our website.

Latest Posts

Set Custom CSS IDs for Individual Divi Accordion Items

Assigning unique CSS IDs to specific Divi Accordion items allows for precise control over styling, targeting, and linking within your page content. This ability is particularly useful when you want to apply custom designs or create anchor links to particular accordion...

Enable Swipe Navigation in the Divi Gallery Lightbox

Enabling swipe navigation in the Divi Gallery module's lightbox allows users to seamlessly browse through gallery images by swiping left or right, creating a more interactive and mobile-friendly experience. This functionality can significantly improve user engagement...

Disable Slide-In Animation for Divi Gallery Grid Images

Control how images appear in your Divi Gallery module by toggling the slide-in animation effect for grid layouts. Disabling the slide-in animation allows gallery images to load instantly and appear statically, providing a faster and distraction-free browsing...

Control Image Count Display in Divi Gallery Lightbox

Displaying or hiding the image count in the Divi Gallery module’s lightbox can help customize the user experience, depending on whether you want to give visitors an indication of gallery progress or prefer a cleaner, distraction-free view. The ability to toggle this...

Hide Gallery Image Titles in the Divi Lightbox Overlay

Displaying image titles in the lightbox overlay of the Divi Gallery module can sometimes be distracting or unnecessary, depending on your website’s design and user experience goals. Hiding these titles creates a cleaner and more focused viewing experience for visitors...

Random Posts