include and get_template_part
Asked Answered
P

2

7

What is the difference between a php include and the wordpress get_template_part other then you have a fallback option on the latter? Can I as well use a php include instead?

Population answered 28/3, 2013 at 18:28 Comment(0)
A
3

I was also wondering about this recently and the article that I found very useful was this:

How to Include and Require Files and Templates in WordPress

The author of the article follows these guidelines:

  • In theme development, I always use get_template_part().
  • In plugin development, I almost always use include_once() and I generally use it once in a function. You can see this in my boilerplates.
  • If the plugin I’m writing is going to be used on a very large site, then I use require_once().

UPDATE

According to another article, using get_template_part() is especially useful when trying to build a child theme, but I guess you already know that:).

Maybe the best way to know the difference is to check the source code of get_template_part()

Acrobatic answered 28/3, 2013 at 18:41 Comment(1)
Thanks... but I don't see why the author thinks that get_template_part('loop' 'standard'); is a better solution than include( 'loop-standard.php' );Population
N
2

get_template_part also allows child themes to override that file and include additional more specific files.

For example, if you used get_template_part( 'loop', 'single' ) and your theme only has a file named loop.php then a child theme could include a file named loop-single.php that would override your loop.php.

Nay answered 29/4, 2021 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.