Add Author Profile Picture in the Divi Blog Module

Written by Dan Mossop

When browsing blogs, it's nice to see a face attached to the words you're reading. A picture of the author can be the difference between a post that feels sterile and one that feels welcoming. For Divi users, adding an author's profile picture beside each blog post in the Divi Blog Module isn't an in-built feature – but that doesn't mean it can't be done. Let's walk through how to add an author profile picture (a.k.a. avatar) in the Divi Blog Module to give your posts that personal flare.

Here's the before and after of what we're trying to achieve:

Before

After

Add an Author Profile Picture to the Divi Blog Module using PHP / CSS

To add the author avatar to the blog module, we'll start with some PHP to add the profile images into the blog module output. The following PHP code does this – it first scans the blog module HTML code for the post IDs of the displayed posts, then uses these to retrieve the corresponding author image, before inserting the author image into a new container element along with the post title. 

Here's the PHP code:

function divi_booster_add_author_avatar_to_blog_module($output, $render_slug, $module) {
    if ('et_pb_blog' !== $render_slug || !is_string($output)) {
        return $output;
    }
    
    return preg_replace_callback('/(<article id="post-(\d+)"[^>]*>)(.*?)(<h2 class="entry-title">.*?<\/h2>)/s', 'divi_booster_insert_author_avatar', $output);
}

function divi_booster_insert_author_avatar($matches) {
    $post_id = isset($matches[2]) ? intval($matches[2]) : false;
    if (!$post_id) {
        return $matches[0];
    }

    $author_id = get_post_field('post_author', $post_id);
    $author_url = get_author_posts_url($author_id);
    $author_name = get_the_author_meta('display_name', $author_id);
    $avatar = get_avatar($author_id);
    
    // Wrap the avatar in an anchor tag that links to the author archive page
    // and includes the title and rel attributes.
    $avatar_link = '<a href="' . esc_url($author_url) . '" title="Posts by ' . esc_attr($author_name) . '" rel="author">' . $avatar . '</a>';
    $author_avatar_html = '<div class="divi-booster-author-avatar">' . $avatar_link . '</div>';

    // Wrap the avatar and the entry heading in a new container.
    $header_html = '<div class="divi-booster-entry-header">' . $author_avatar_html . $matches[4] . '</div>';

    return $matches[1] . $matches[3] . $header_html;
}

add_filter('et_module_shortcode_output', 'divi_booster_add_author_avatar_to_blog_module', 10, 3);

Add the PHP code to your child theme's functions.php or a custom plugin.

To ensure the avatar appears neatly aligned with the post title, we add some CSS:

.et_pb_posts .divi-booster-entry-header {
    display: flex;
    align-items: center;
}

.et_pb_posts .divi-booster-author-avatar img {
    border-radius: 50%; /* Optional: if you want a round avatar */
    height: 30px;
    min-width: 30px;
    width: 30px;
}

.et_pb_posts .divi-booster-author-avatar {
    margin-right: 15px; /* Adjust the space between the avatar and the title */
}

The CSS styles the avatar, pacing it inline with the title and ensures that it has a nice, circular appearance with some space between it and the post title.

Your Divi blog posts should now display the author's avatar alongside the title, giving your website a warm, personalized touch.

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, 

0 Comments

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

Hide the Page Title in the Hello Elementor Theme

Hiding the page title in the Hello Elementor theme allows you to create a cleaner and more customized page layout, free from default headings that may not fit your design. This is particularly useful for landing pages or custom content layouts where the default page...

How to Hide a Divi Module When Scrolling Up or Down

In this quick tutorial, I’ll show you how to hide a Divi module when scrolling in a specific direction (up or down), so that the module only shows when you want it to. This is especially useful when using Divi’s Scroll Effects feature and you have a effect you want to...

Fade a Divi Image Module Edge into the Background

Want to create a stylish fade effect on your Divi image module—where one side fades smoothly into the background? With a bit of CSS, you can make any edge (or corner) of the image fade out: top, bottom, left, right, or even diagonally.Fade a Divi Image Module Edge...

Hide the Header and Footer in the Hello Elementor Theme

Removing the default header and footer from your Hello Elementor theme allows for a streamlined and distraction-free website design. This is especially useful when creating unique landing pages, full-width layouts, or custom headers and footers with a page builder. In...

Setting up the Divi Password Box Module

Setting up password protection on a page can help you control access to sensitive or private content in WordPress, allowing only authorized visitors to view certain sections. With the Divi Password Box module, you can replace the plain Divi password form with a fully...

Random Posts