Last updated on October 18, 2023

Divi blog post related posts

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Display related posts in Divi blog.

To display related posts at the end of a Divi blog post, you can use the get_related_posts() function along with a custom Divi module. This will allow you to fetch and display a list of related posts based on a set of criteria, such as shared categories or tags.

Here’s an example code snippet that demonstrates how to achieve this functionality:

function wpsnippets_get_related_posts() {
    // Get the current post ID
    $post_id = get_the_ID();

    // Get the categories of the current post
    $categories = wp_get_post_categories($post_id);

    // Get the tags of the current post
    $tags = wp_get_post_tags($post_id);

    // Set up the query arguments
    $args = array(
        'post__not_in' => array($post_id), // Exclude the current post
        'posts_per_page' => 5, // Number of related posts to display
        'category__in' => $categories, // Only show posts from the same categories
        'tag__in' => $tags, // Only show posts with the same tags
        'orderby' => 'rand', // Randomize the order of related posts
    );

    // Query the related posts
    $related_posts = new WP_Query($args);

    // Check if there are any related posts
    if ($related_posts->have_posts()) {
        // Start the output buffer
        ob_start();

        // Loop through the related posts
        while ($related_posts->have_posts()) {
            $related_posts->the_post();
            // Display the related post title and link
            echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br>';
        }

        // End the output buffer and return the related posts
        return ob_get_clean();
    }

    // If no related posts are found, return an empty string
    return '';
}

To use this code snippet, you can create a custom Divi module and add the wpsnippets_get_related_posts() function to it. This will allow you to easily insert the related posts section into your Divi blog post layout.

Please note that this code assumes you have the Divi theme installed and activated on your WordPress site. Additionally, you may need to customize the HTML markup and styling of the related posts section to match your specific design requirements.

Examples

Example 1: Displaying related posts based on category

This use case demonstrates how to display related blog posts in a Divi theme blog post based on the category of the current post. The code example below uses the get_the_category() function to retrieve the category of the current post, and then queries for other posts in the same category to display as related posts.

<?php
$category = get_the_category();
if ($category) {
    $category_ids = array();
    foreach ($category as $cat) {
        $category_ids[] = $cat->term_id;
    }
    $args = array(
        'category__in' => $category_ids,
        'post__not_in' => array($post->ID),
        'posts_per_page' => 3,
        'orderby' => 'rand'
    );
    $related_posts = new WP_Query($args);
    if ($related_posts->have_posts()) {
        while ($related_posts->have_posts()) {
            $related_posts->the_post();
            // Display related post content here
        }
        wp_reset_postdata();
    }
}
?>

The code first retrieves the category of the current post using get_the_category(). It then creates an array of category IDs and uses them in the WP_Query arguments to query for related posts. The post__not_in parameter ensures that the current post is not included in the related posts. Finally, the code loops through the related posts and displays their content.

Example 2: Displaying related posts based on tags

This use case demonstrates how to display related blog posts in a Divi theme blog post based on the tags of the current post. The code example below uses the get_the_tags() function to retrieve the tags of the current post, and then queries for other posts with similar tags to display as related posts.

<?php
$tags = get_the_tags();
if ($tags) {
    $tag_ids = array();
    foreach ($tags as $tag) {
        $tag_ids[] = $tag->term_id;
    }
    $args = array(
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'posts_per_page' => 3,
        'orderby' => 'rand'
    );
    $related_posts = new WP_Query($args);
    if ($related_posts->have_posts()) {
        while ($related_posts->have_posts()) {
            $related_posts->the_post();
            // Display related post content here
        }
        wp_reset_postdata();
    }
}
?>

The code first retrieves the tags of the current post using get_the_tags(). It then creates an array of tag IDs and uses them in the WP_Query arguments to query for related posts. The post__not_in parameter ensures that the current post is not included in the related posts. Finally, the code loops through the related posts and displays their content.

Example 3: Displaying related posts based on custom taxonomy

This use case demonstrates how to display related blog posts in a Divi theme blog post based on a custom taxonomy. The code example below assumes you have a custom taxonomy called “genre” and uses the get_the_terms() function to retrieve the terms of the “genre” taxonomy for the current post. It then queries for other posts with the same terms to display as related posts.

<?php
$terms = get_the_terms($post->ID, 'genre');
if ($terms) {
    $term_ids = array();
    foreach ($terms as $term) {
        $term_ids[] = $term->term_id;
    }
    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'genre',
                'field' => 'term_id',
                'terms' => $term_ids
            )
        ),
        'post__not_in' => array($post->ID),
        'posts_per_page' => 3,
        'orderby' => 'rand'
    );
    $related_posts = new WP_Query($args);
    if ($related_posts->have_posts()) {
        while ($related_posts->have_posts()) {
            $related_posts->the_post();
            // Display related post content here
        }
        wp_reset_postdata();
    }
}
?>

The code first retrieves the terms of the “genre” taxonomy for the current post using get_the_terms(). It then creates an array of term IDs and uses them in the tax_query parameter of the WP_Query arguments to query for related posts. The post__not_in parameter ensures that the current post is not included in the related posts. Finally, the code loops through the related posts and displays their content.

Last updated on October 18, 2023. Originally posted on December 1, 2023.

Leave a Reply

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