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_post_format()
is to determine if a post has any post format.It returns the string value of the current post format type, which is useful in several ways. One of the most powerful is to call different template part files based on post format, e.g.:
get_template_part( 'entry', get_post_format() )
Which will include, e.g.
entry-aside.php
for an aside format, orentry.php
for a standard format.Source: https://wordpress.stackexchange.com/questions/14257/has-post-format-vs-get-post-format
get_template_part( 'content', get_post_format() );
yes, in your theme directory there should be a template file content.php, for including it by
get_template_part()
get_post_format()
Returns the post format of a post. This will usually be called in the the loop, but can be used anywhere if a post ID is provided.
more
get_template_part()
will indeed call for a template to be included, which is a safer version of PHP’sinclude()
as it will silently fail if no template is found.It follows WordPress’es
slug
namespacing for template files.Which you’ve defined
content
as yourslug
thus it’ll look for:Where as, your second function,
get_post_format()
will call the second part. The post format is defined by the post itself, depending on the metadata applied to the post itself,i.e. PostID 221 is a "quote"
This is great for “Sections” of the page, where your post may include an uploaded Video or an quote from an external author, thus bring in an external format to be used across the site, as
get_post_format()
docs describes the list of possiblepost_format()'s
:If you actually just wanted to get a straight forward template part for post content, calling:
Will look for a template file called
single.php
, although if you wanted to author your own bespokecontent-*
template files:if you want to display specific category wise post then you can use this code
Where $ID is a category id and $NUMBEROFPOSTS is you want to display number of posts in home page specific category .
On last echo $ variable name then it will display all the content from category posts.