By default, the Blog Module doesn't include sharing icons for each post it displays; however, with the below customization, we can create custom sharing icons that will be displayed on each post.
Pro Tip: To ensure that each sharing icon will be using the correct post information, the Rank Math plugin can also be used to configure the sharing details such as:
The Post title
The Post Image
The Post description
Install and activate the Rank Math plugin.
For each of the posts, set the Social Media Snippet.
Note: if no image is selected, the Post's featured image will be used instead.
Open the Blog Module's settings, go to Advanced Tab → CSS ID & Classes → CSS Class and type in
divi_blog_with_sharing_icons
.To ensure that the following script will only run on specific pages, right after the Blog module in the page layout, add a Code module and place the following script inside it:
<script id="divi-insert-social-icons-for-blog-module-posts">
function addSocialIcons() {
// Check if the wrapper element exists
if (!document.querySelector('.divi_blog_with_sharing_icons')) {
return;
}
// Function to create a social sharing link
function createSocialLink(url, className, iconClass) {
var a = document.createElement('a');
a.href = '#';
a.rel = 'noopener noreferrer';
a.className = className;
// Set innerHTML to include an <i> tag with the specified icon class
a.innerHTML = '<span class="' + iconClass + '"></span>';
a.addEventListener('click', function (event) {
event.preventDefault();
openPopupCenter(url, iconClass, 600, 400);
});
return a;
}
function openPopupCenter(url, title, w, h) {
// Calculate the position to center the popup
var left = screen.width / 2 - w / 2;
var top = screen.height / 2 - h / 2;
// Open a new window with specified dimensions and positions
window.open(
url,
title,
'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=' +
w +
', height=' +
h +
', top=' +
top +
', left=' +
left,
);
}
// Select all posts
var posts = document.querySelectorAll('.divi_blog_with_sharing_icons .et_pb_post');
// Iterate over each post
posts.forEach(function (post) {
// Avoid adding icons multiple times
if (post.querySelector('.divi-social-share-icons-wrapper')) return;
// Extract post details
var title = post.querySelector('.entry-title').textContent;
var postUrl = post.querySelector('.entry-title a').href;
// Create sharing URLs
var facebookUrl =
'https://www.facebook.com/sharer/sharer.php?u=' +
encodeURIComponent(postUrl);
var twitterUrl =
'https://twitter.com/intent/tweet?text=' +
encodeURIComponent(title) +
'&url=' +
encodeURIComponent(postUrl);
var linkedInUrl =
'https://www.linkedin.com/sharing/share-offsite/?url=' +
encodeURIComponent(postUrl);
// Create social icons
var socialContainer = document.createElement('div');
socialContainer.className = 'divi-social-share-icons-wrapper';
socialContainer.appendChild(
createSocialLink(
facebookUrl,
'divi-social-icon-link',
'divi-icon-facebook',
),
);
socialContainer.appendChild(
createSocialLink(
twitterUrl,
'divi-social-icon-link',
'divi-icon-twitter',
),
);
socialContainer.appendChild(
createSocialLink(
linkedInUrl,
'divi-social-icon-link',
'divi-icon-linkedin',
),
);
// Determine the element to insert the social icons after
var insertAfterElement;
var featuredImageUrlExists = post.querySelector('.entry-featured-image-url');
var blogGridWrapperExists = document.querySelector('.et_pb_blog_grid_wrapper');
var imageContainer = post.querySelector('.et_pb_image_container');
if (featuredImageUrlExists && blogGridWrapperExists) {
insertAfterElement = imageContainer;
} else if (featuredImageUrlExists && !blogGridWrapperExists) {
insertAfterElement = featuredImageUrlExists;
} else {
insertAfterElement = post.querySelector('p.post-meta');
}
// Insert the icons after the determined element
insertAfterElement.appendChild(socialContainer);
});
}
// Run on initial page load
document.addEventListener('DOMContentLoaded', addSocialIcons);
// Run on AJAX completion
$(document).ajaxComplete(addSocialIcons);
</script>Edit the Blog module, go to Advanced Tab → Custom CSS → Free-Form CSS and paste in this CSS code:
selector .divi-social-share-icons-wrapper {
position: absolute;
bottom: 10px;
padding-left: 10px;
}
selector .et_pb_no_thumb .divi-social-share-icons-wrapper {
position: relative;
bottom: auto;
padding-left: 0;
padding-top: 10px;
}
selector .divi-social-share-icons-wrapper .divi-social-icon-link span::before {
font-family: ETModules;
font-size: 16px;
color: white;
padding: 4px 8px;
border-radius: 5px;
display: inline-block;
margin-right: 10px;
}
selector .divi-social-share-icons-wrapper .divi-social-icon-link .divi-icon-facebook::before {
content: "";
background-color: #3b5998;
}
selector .divi-social-share-icons-wrapper .divi-social-icon-link .divi-icon-twitter::before {
content: "";
background-color: #000000;
}
selector .divi-social-share-icons-wrapper .divi-social-icon-link .divi-icon-linkedin::before {
content: "";
background-color: #007bb6;
}
The result:
The result of clicking on the custom sharing icons, if for each post, the correct OG tags were set using the Rank Math plugin: