How to Display Post Formats in Your WordPress Theme

Home » Snippets » How to Display Post Formats in Your WordPress Theme
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to enhance the visual design of your WordPress theme by displaying post formats? Post formats allow you to vary the presentation of different types of posts, such as gallery, video, audio, quote, and more. By incorporating post formats into your theme, you can create a visually engaging website that showcases your content in a unique and distinctive way. In this article, we will explore how to properly display post formats in your WordPress theme, so that you can take your website to the next level of aesthetic appeal.

					function wpturbo_display_post_format() {
    $format = get_post_format();
    switch ($format) {
        case 'aside':
            echo '<span class="post-format">Aside</span>';
            break;
        case 'image':
            echo '<span class="post-format">Image</span>';
            break;
        case 'video':
            echo '<span class="post-format">Video</span>';
            break;
        case 'audio':
            echo '<span class="post-format">Audio</span>';
            break;
        case 'quote':
            echo '<span class="post-format">Quote</span>';
            break;
        case 'link':
            echo '<span class="post-format">Link</span>';
            break;
        case 'gallery':
            echo '<span class="post-format">Gallery</span>';
            break;
        case 'status':
            echo '<span class="post-format">Status</span>';
            break;
        case 'chat':
            echo '<span class="post-format">Chat</span>';
            break;
        default:
            echo '';
            break;
    }
}

add_action('wp', 'wpturbo_display_post_format');
				

The code snippet provided is a function called wpturbo_display_post_format(), which is responsible for displaying the post format of a WordPress post in your theme.

To begin, the function uses the get_post_format() function to retrieve the post format of the current post. The post format is stored in the variable $format.

Next, a switch statement is used to determine the appropriate HTML markup for each post format. Within each case, a corresponding <span> element with a class of post-format is created, and the name of the post format is echoed onto the webpage.

For example, in the case of an ‘aside’ post format, the following HTML will be echoed:

<span class="post-format">Aside</span>

This pattern is repeated for each of the supported post formats, including ‘image’, ‘video’, ‘audio’, ‘quote’, ‘link’, ‘gallery’, ‘status’, and ‘chat’. Each post format has its own <span> element with the post format name inside.

Finally, a default case is included to handle any other post format that might not be explicitly mentioned in the switch statement. In this case, an empty string is echoed, meaning that there will be no post format displayed.

To ensure that the function is executed when needed, the add_action() function is used to hook the wpturbo_display_post_format() function to the 'wp' action. This means that whenever a WordPress page is being rendered, the function will be called and the post format will be displayed accordingly.

In summary, this code snippet provides a way to display the post format of a WordPress post in your theme by utilizing the get_post_format() function and a switch statement to output the appropriate HTML markup.

Register an account to save your snippets or go Pro to get more features.