Last updated on October 18, 2023

Divi blog post tags

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

Display tags for blog posts in Divi.

The Divi theme is a popular WordPress theme that allows you to create beautiful and customizable websites. One of the features of the Divi theme is the ability to display tags on your blog posts. Tags are a way to categorize and organize your blog posts, making it easier for visitors to find related content.

To display tags on your Divi blog posts, you can use the following code snippet:

function wpsnippets_display_tags() {
    $tags = get_the_tags();

    if ( $tags ) {
        echo '<div class="post-tags">';
        echo '<span class="tags-title">Tags:</span>';

        foreach ( $tags as $tag ) {
            $tag_link = get_tag_link( $tag->term_id );
            echo '<a href="' . $tag_link . '" rel="tag">' . $tag->name . '</a>';
        }

        echo '</div>';
    }
}

This code snippet defines a custom function wpsnippets_display_tags() that retrieves the tags associated with the current blog post using the get_the_tags() function. It then checks if there are any tags available and if so, it loops through each tag and displays it as a clickable link using the get_tag_link() function.

To use this code snippet, you can add it to your theme’s functions.php file or create a custom plugin. Once added, you can call the wpsnippets_display_tags() function within your Divi blog post template to display the tags.

This code snippet is useful if you want to enhance the user experience on your Divi blog posts by providing a way for visitors to easily navigate to related content based on tags. It helps in organizing your blog posts and improving the overall usability of your website.

Examples

Example 1: Displaying Divi blog post tags in a custom location

This use case demonstrates how to retrieve and display the tags of a Divi blog post in a custom location within the post content.

<?php
function wpsnippets_display_divi_tags() {
    $post_tags = get_the_tags();
    if ( $post_tags ) {
        echo '<div class="custom-tags">';
        foreach ( $post_tags as $tag ) {
            echo '<a href="' . get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a>';
        }
        echo '</div>';
    }
}

Explanation:

  • The get_the_tags() function retrieves the tags associated with the current post.
  • If there are tags available, a custom <div> container is created with the class “custom-tags”.
  • A foreach loop is used to iterate through each tag and display it as a clickable link.
  • The link URL is obtained using the get_tag_link() function, passing the tag’s term ID as a parameter.
  • Finally, the closing </div> tag is echoed to close the custom tags container.

Example 2: Styling Divi blog post tags with CSS

This use case demonstrates how to style the Divi blog post tags using custom CSS.

.custom-tags a {
    background-color: #f2f2f2;
    color: #333;
    padding: 5px 10px;
    border-radius: 3px;
    margin-right: 5px;
    text-decoration: none;
}

.custom-tags a:hover {
    background-color: #333;
    color: #fff;
}

Explanation:

  • The .custom-tags a selector targets the anchor tags within the custom tags container.
  • CSS properties such as background-color, color, padding, border-radius, margin-right, and text-decoration are applied to style the tags.
  • The .custom-tags a:hover selector targets the anchor tags when hovered, allowing for additional styling.

Example 3: Filtering Divi blog posts by tag

This use case demonstrates how to create a custom query to display only Divi blog posts that have a specific tag.

<?php
function wpsnippets_filter_divi_posts_by_tag() {
    $tag_id = 5; // Replace with the desired tag ID
    $args = array(
        'post_type' => 'post',
        'tag__in'   => array( $tag_id ),
    );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            // Display the post content or any other desired output
        }
        wp_reset_postdata();
    }
}

Explanation:

  • The $tag_id variable should be replaced with the desired tag ID. This ID can be obtained from the WordPress admin area or programmatically.
  • The $args array is used to define the custom query parameters. In this case, we specify the post type as “post” and the desired tag using the tag__in parameter.
  • The WP_Query class is used to create a new query object with the specified arguments.
  • The while loop is used to iterate through each post in the query result.
  • Inside the loop, you can display the post content or any other desired output.
  • Finally, wp_reset_postdata() is called to restore the global post data to its original state.
Last updated on October 18, 2023. Originally posted on January 6, 2024.

Leave a Reply

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