WordPress get_template_part Tutorial

by birtanpublished on July 3, 2020

hello everyone welcome to another

WordPress tutorial in previous lessons

we learned how to control the output of

our blog homepage we learned how to

control the output of archive listings

and we learned how to control the output

of search results but what we didn't

learn is how to keep our code

dry dry stands for don't repeat yourself

so you'll notice that there's a lot of

similarities between the way that posts

are output on the search results

category archives in the home page

they're all outputting the title with a

link the date the author the category

and then either the content or the

excerpt so there's no need to have that

code repeated in our theme files so in

this lesson we're going to learn about a

function named get template part which

will allow our code to be very dry so

let's dive right in

so we'll hop over to our code this is

indexphp of our theme folder and what

we want to do is extract the part of the

code that is being looped through so we

can see here's while this line of code

anything below this line of code but

above this end wild line is what's being

looped through so basically the article

element so we want to take this article

element and cut it and then create a new

file in our theme folder named content

dot PHP and then just paste in the code

that we just removed from index okay so

now where that code was sitting we want

to enter a new line that says get

template part this is a wordpress

function it accepts two parameters for

now we're only going to use one and

we'll say content obviously this slug

matches the name of the new file that we

just created so it will pull in this

code so we said content close the line

with a semicolon and then because we

removed HTML and we added PHP we just

need to drop into and out of PHP at the

correct moments so I'll edit that okay

we're looking good now if i refresh

quite literally nothing changes which is

good we're not looking to change the

output or behavior of our theme we just

want the code to be less repetitive

so now we can reuse this line get

template part content and other theme

files so for example when we perform a

search on our site so if I search for

the phrase opinion this output is being

controlled by search PHP now we're in

search PHP and this code looks really

familiar this code is nearly identical

beginning with article this is what's

getting looped through it's nearly

identical to what was in index dot PHP

which means we don't need this so I can

delete this and then paste in git

template part and obviously drop into

and out of PHP where it makes sense if i

refresh the search results screen we see

very little change but let's go ahead

and review the tiny change that we did

see so we noticed that now we see the

full text for this post whereas before

we substituted the code with git

template part for search PHP search

results were always only returning a

short excerpt let's restore that

functionality so in content dot PHP

scroll down here this is the file that's

being included with the git template

part code and we want to adjust this so

that when we're on the home page it uses

this existing logic to decide between

showing either the excerpt or the full

text but if we're on the search results

page we want it to always be the excerpt

so we can just create a new if statement

that will do that for us so we'll say it

will drop into PHP and say if we're

currently in a search so we'll say is

search so we'll include certain code in

here otherwise else will include other

code here so here's the code for the

excerpt so we always only want that if

we're in a search so that goes into

these brackets drop out of PHP and then

back into PHP here and then this entire

logic that decides between the excerpt

or the full content when we're on the

home page can go in these brackets so

we'll just copy and paste this here and

then obviously we don't need to drop


P again and then we don't need to drop

out of it again okay so now if we

refresh and we can see that the search

results page is always only showing an

excerpt but the home page is still using

the logic to decide whether to use the

excerpt of the full content but the neat

thing is is that both search and index

they're both being powered by git

template part which means we're keeping

our code dry we're not repeating

ourselves unnecessarily we're letting

contentphp power the output for both

views and where there is a tiny bit of a

difference we're just using an if

statement instead of having two separate

files completely let's make our code

even less repetitive you'll remember

that we also have an archive view so if

I click on opinion it'll only show posts

from the opinion category now that's

being controlled by archive dot PHP and

it also has very similar code so you'll

remember this article code that we

already created a separate file for so I

will remove article and instead write

get template part content

so here we can see that if we refresh

archive dot PHP this is the archive view

for opinion it is now using the exact

same code as the home page and the

search page now if we stop and think for

a moment we'll probably conclude that

the archive should be similar to the

search page and that we only want to

show excerpts no matter what so what we

can do in content dot PHP you remember

that if statement that we created we're

at saying if we're currently viewing a

search then always show the excerpt we

can simply say or if we're in an archive

and then if we refresh you can see that

now archives will always use the excerpt

okay so let's review what we've

accomplished so far we had code that was

nearly identical in indexphp and we

replaced that search dot PHP and we

replaced that an archive dot PHP we

replaced all three of those with a

single line get template part content

which looks in the content dot PHP file

that we created so now all of our code

is in one central place and it's not

repeated throughout our theme files and

this is fantastic because now if we ever

want to make any changes we can make

them once instead of having to repeat

ourselves three times now before we

close out this lesson you'll remember

that earlier I mentioned that this

function get template part accepts two

parameters not just one now obviously in

this lesson we've only seen the first

parameter in use the first parameter is

required that's the slug and it's the

base file name that WordPress will look

for so this code obviously like we've

said before will make WordPress look for

content PHP in our theme folder now the

second parameter that's optional allows

you to specify a more particular name so

if I say home or single or page but

let's put it back to home WordPress will

now try to include or require a file

named content – home dot PHP now that

might not be very useful but let me show

you something that we can play with in

the future that would be very useful

instead of quotes we could say get post

format so what that will do is it's a

function that will return what type of

post is currently being looped through

so it will be either a standard post or

an aside or a photo gallery or an audio

file and if future lessons will learn

more about WordPress and post formats

but for now just know that the git

template part function allows you to

pass along a second parameter that can

make things much more dynamic that will

bring this particular lesson to a close

the takeaway point should be that the

git template part function allows your

theme to avoid repetitious code but even

beyond that it allows you as the

developer to stay organized to break out

code into certain chunks and certain

files so it's easier to manage and we'll

learn more about that in the future so

thank you very much for watching I hope

you feel like you learn something and

stay tuned for more wordpress and web

development tutorials thanks bye

Related Videos

Be the first to comment “WordPress get_template_part Tutorial”

Your email address will not be published.

There are no comments yet.