How do you call a template in WordPress?

To call a template in WordPress, the best function that WordPress introduces is:

get_template_part();

However, this WordPress function has parameters. And we come to that point in a minute. Before that, we need to understand first why we need this function at all?

The main reason behind using this function indicates to an acronym DRY, which is very popular concept in programming world. It means Don’t Repeat Yourself.

Certainly, we want to minimize the code lines, and we don’t want to repeat our codes. But, to that, we could have used PHP include or require functions.

Then why should we use get template part function in WordPress?

How do I get template parts in WordPress?

There are reasons why we should use get template part function instead of PHP include and require functions.

We’ll understand that in a minute. But, before that let’s see how we can get the template parts in WordPress.

Above all, the name of the WordPress function is tells us what it’s going to do. This function will get template parts.

That means, if we have a chunk of code that repeats itself in more than one file, we can use that chunk of code. And use them as a part of our template.

The following chunk of code we use in both single and page PHP file.

<div id="body">

    <?php while(have_posts()) {
        the_post(); ?>
        <h1 class="pb-4 mb-4 fst-italic border-bottom">
        <?php the_title(); ?>
      </h1>
      <p class="blog-post-meta">
          
          Posted by 
    <?php the_author_posts_link(); ?>
    on
    <?php the_time( 'd.m.y' ) ?>
    in
    <?php echo get_the_category_list(', '); ?>
        </p>
<p>
	<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'full' );
}
?>
		</p>	
			<article class="blog-post">
      <?php the_content( ); ?>
      </article> 

    <?php
  }
 
  ?>
	
</div>

Now, we can save this chunk of PHP code in a PHP file called content.

As a result now we can use this PHP file content as a part of our template.

So, the code of single and page PHP file both now look like the following.

<?php

get_header( );

get_template_part( 'content' );

get_footer( ); 

?>

However, we can change the look of the PHP file page, as the get template part function allows us to pass the second parameter.

WordPress get_template_part pass variable

That’s the magic of WordPress get template part function. This function can pass the second parameter.

To make it happen, we can create another PHP file and name it content-second.

<?php if(have_posts()): while(have_posts(  )): the_post();  ?>

<div class="row">

<div class="col-lg-3">This is Sidebar</div>

<div class="col-lg-9">

<article class="blog-post-title">
<?php the_title( ); ?>
</article>

<article class="blog-post">
    <?php the_content( ); ?>
</article>

</div>

</div>

<?php endwhile; else: endif; ?>

As we’re going to use a different header with this file, the above code differs from the earlier PHP template file content.

Now, the PHP file page that defines the style of every WordPress page, has a different chunk of code than the WordPress file single.

<?php

get_header( 'front' );

get_template_part( 'content', 'second');

get_footer( 'front' ); 

?>

As we can clearly see that this WordPress file uses a different header and footer. We’ve discussed the topic earlier.

For that reason, now any page looks different than the blog post.

However, WordPress file single that represents the blog post pages doesn’t have any sidebar.

Finally, we’ll see why it’s better than PHP include or require functions.

When we pass two parameters inside the get template part function, the first parameter always acts as a fallback.

For example, if the WordPress file page doesn’t fine front-second template files, it will use the front template file.

That never happens with the PHP functions include and require.

What Next

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Technical blog

Twitter


Posted

in

by

Comments

4 responses to “How do you call a template in WordPress?”

  1. […] About template, we’ve written a post before, please check it. […]

  2. […] We have written about get template part function of WordPress earlier. […]

  3. […] and fallback, both. If you have a web development background, then you may take an interest in how WordPress used fallback while using get template part […]

  4. […] and fallback, both. If you have a web development background, then you may take an interest in how WordPress used fallback while using get template part […]

Leave a Reply