Today we are going to explore WordPress Post formats. So in WordPress if you want to show some custom content looking a bit different in style than usual posts then you can use post-formats, It will allow you to show highlighted content or image gallery or link to some website with styles, etc.
There are a couple of post formats we can use in WordPress.
We are going to add a few of these post formats into our custom theme.
So let's get started.
To add post format in any theme we can go into the "functions.php" file and there we can add this code to enable post-formats.
// add Features function addFeaturesInTheme(){ // Add post formats support add_theme_support('post-formats',array('aside','gallery','link')); } // enabling feature after theme setup add_action('after_setup_theme','addFeaturesInTheme');
Once you have this code saved you should see post formats in your add/edit post page.
Now add images in your gallery after selecting "Gallery" as given in the image. and in theme, you need to create a "content-gallery.php" file.
For more clarity check our Video Tutorial
Notice here we are creating file "content-gallery", As we are dealing with gallery post so we added "gallery" in file name. But content came from "get_template_part" function. Have a look
if(have_posts()): while(have_posts()): the_post(); get_template_part('content',get_post_format()); endwhile; endif;
So we are using loop content from the "content" file.
If you are not aware of "get_template_part" then check here
Mainly our gallery will be printed using the "get_post_format" WordPress function. as given in the code above.