Display Post Format in WordPress Theme

In one of my earlier post, I showed how to add post format support for your theme. Using post formats allows users to use certain class that can be style anyway you want using simple CSS. You can retrieve the post class using the following function within a "div" element.

<?php post_class(); ?>

This function should display all the possible lists of classes that has been assigned to your post. It should look something like this screenshot attached below.

WordPress Post FormatSource Code Reveals the Post Format.

As you can see from this screenshot, I was using "standard" post format which assigned "format-standard" class on my "article" element. Perhaps one limitation of using the function mentioned above is that it reveals all the classes which you think "not necessary". Here is the simple solution for you.

if (!empty(get_post_format())) {
   echo get_post_format(); 
} else { 
   echo 'standard'; } 

This function should only reveal the name of the post format in one word. This could really be useful (just like before) if you want to avoid all the unnecessary classes that you won't need. It also gives you a more precise and clear class without fearing that you might overlap with other custom classes that you may have on your CSS file. Hope you get the idea.

References: get_post_format, post_class.


Commenting is disabled.