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.

<?php 
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.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.