Yes, it is possible. The easiest and very readable way would be to do this like TwentyX themes do – using get_post_format
and get_template_part
functions.
Let’s say that your current single.php
file looks something like this:
<?php get_header(); ?>
...
<?php while ( have_posts() ) : the_post; ?>
<article ...>
...
</article>
<?php endwhile; ?>
...
Just change it to:
<?php get_header(); ?>
...
<?php
while ( have_posts() ) : the_post;
get_template_part( 'content', get_post_format() );
endwhile;
?>
...
And move this part:
<article ...>
...
</article>
to file called content.php
;
Now, this will be the default HTML for your post. If you want to modify the HTML code for… let’s say quote format, then you’ll have to create new file called content-quote.php
(so the rule here is (content-{post_format}.php
).
Why and how does it work?
get_post_format
returns the post format of a post.
get_template_part
on the other hand makes all the magic 😉 Citing from Codex:
Provides a simple mechanism for child themes to overload reusable
sections of code in the theme.Includes the named template part for a theme or if a name is specified
then a specialised part will be included. If the theme contains no
{slug}.php file then no template will be included.The template is included using require, not require_once, so you may
include the same template part multiple times.For the $name parameter, if the file is called “{slug}-special.php”
then specify “special”.