Add Author Profile Picture in the Divi Blog Module

|

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.

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.