The get_template_part() function does exactly what you imagine. It includes part of a template within another template. For instance within index.php you could place the following code, borrowed directly from the TwentyTwelve theme.
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( ‘content’, get_post_format() ); ?>
<?php endwhile; ?>
The above code actually pulls selectively chosen content for different post formats so we can easily display only what we want for each post format and focus on what’s important to that content type.
It pulls selective content because it makes use of the get_post_format() function to figure out what type of post is being processed by the loop. Combining that with get_template_part() makes for a very powerful default loop.
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( ‘content’, get_post_format() ); ?>
<?php endwhile; ?>
The above code actually pulls selectively chosen content for different post formats so we can easily display only what we want for each post format and focus on what’s important to that content type.
It pulls selective content because it makes use of the get_post_format() function to figure out what type of post is being processed by the loop. Combining that with get_template_part() makes for a very powerful default loop.
Labels:
Wordpress
Previous Article

Responses
0 Respones to "What is get_template_part()?"
Post a Comment