I am pretty new in WordPress theme development and I have some doubt about this WP function used to show my post in the homepage:
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next post navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
Intuitively I understood that as long as there are posts these are displayed in the homepage.
My doubt is related about this line of code:
get_template_part( 'content', get_post_format() );
Reading the documentation it seems to me that:
1) get_template_part: Load a template part into a template. So I think that by this line I am including a template part used to show a post (the structure of a post into my homepage), is it correct?
2) What exactly do get_post_format() ?
get_template_part()
is including a template file from the active theme directory.For example:
get_template_part('content')
will include the templatecontent.php
from the theme folder.When having two parameters, it will search for a template named
firstParameter-secondParameter.php
. So:will include the
content-{$post_format}.php
file.get_post_format() returns one of this defined formats:
or
false
. So the file included will becontent.php
orcontent-format.php
1) Yes!
2) WordPress supports 9 post formats now, let’s say if your theme support 2 of them: ‘aside’ and ‘gallery’, there should be the following code in your functions.php:
You could build different content template part for each post format, and save them as ‘content-aside.php‘ and ‘content-galery.php‘ in your theme folder.
If you don’t have those 2 files in your theme,
get_template_part
will automatically fallback to use the ‘content.php’ instead.All the good WordPressers have already answered your questions, and I think they are successful in eliminating your doubts. So I’m not touching exactly into your question, but to the concepts.
Before starting theme developing, I think a raw theme can be very good to understand the concept behind each specific zones. I started theme development with my [virtual] teacher Ian Stewart’s tutorial on how to develop a theme:
Source Code of the entire tutorial is in Google Code Trunk. I’d suggest you to not just follow the tutorial, but to read the comments too. 🙂
get_template_part()
is a theme function – not necessarily it loads only the post part – rather it can be anything. The code part (a portion of the code) you want to separate from the main file, in PHP we include that usinginclude()
function, but in WordPress, we do it usingget_template_part()
.The code you mentioned, separated the template part where the code to show the post resides. You can comment out that portion and can try the following to understand the basic:
Sorry, I’s not actually with your question, but I tried to pass the concept, and I believe CONCEPT WITH DIFFERENT PERCEPTIONS IS THE KEY TO KNOWLEDGE.
EDIT
You may also start with the
_s
starter theme. Get it directly from GitHub:_s
/underscores
theme – GitHub