Show Post Tags in the Blog Module

|

Divi's blog module lets you display a list of posts. You can also show some details along with the posts, such as the author of the post and the categories the post is in. One thing the blog module doesn't allow you to display is the posts' tags. Here's how you can display post tags in the blog module.

First, here's an example of how a post typically displays in the blog module. As you can see tags are not displayed.

Show Post Tags in the Blog Module using Divi Booster

Divi Booster adds an option for enabling tags in blog modules. With Divi Booster enabled, you'll find the option in the blog module settings at:

Blog Settings > Content > Elements > Show Tags

Enable the option to allow display of tags in the module.

You should now see tags displayed next to your blog module posts, like so:

The option is available in Divi Booster v3.5.5 upwards. It requires PHP 5.3 or higher.

Show Post Tags in the Blog Module using PHP Code

The following code will cause post tags to display in the blog module.

    // === Start: Enable tags in blog module ===
    add_filter('et_pb_blog_shortcode_output', 'dbc_add_blog_module_article_filter');

    function dbc_add_blog_module_article_filter($content) {
        if (is_array($content)) { return $content; }
        return preg_replace_callback('/<article.*?<\/article>/s', 'dbc_apply_blog_module_article_filter', $content);
    }
    function dbc_apply_blog_module_article_filter($match) {
        if (!is_array($match) || !isset($match[0])) { return $match; }
        return apply_filters('dbc_blog_module_article', $match[0]);
    }

    add_filter('dbc_blog_module_article', 'dbc_blog_module_add_tags');

    function dbc_blog_module_add_tags($html) {
        $match = false;
        preg_match('/<article id="post-(\d*)"/', $html, $match);
        $id = isset($match[1])?intval($match[1]):false;
        if (!$id) { return $html; }
        $tags = get_the_tag_list('', ', ', '', $id);
        if (empty($tags)) { return $html; }
        if (strpos($html, '<p class="post-meta">') !== false) {
            $html = preg_replace('/(<p class="post-meta">.*?)(<\/p>)/s', '\\1 | '.$tags.'\\2', $html);
        } else {
            $html = preg_replace('/(<div class="post-content">)/s', '<p class="post-meta">'.$tags.'</p>\\1', $html);
        }
        return $html;
    }
    // === END: Enable tags in blog module ===
Note that this code will apply to all blog modules on the site.

This post may contain referral links which may earn a commission for this site

Hundreds of new features for Divi
in one easy-to-use plugin

0 Comments

Submit a Comment

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