WordPress Archive Tutorial (archive.php)

by birtanpublished on July 3, 2020

hello everyone welcome to another

WordPress tutorial in a previous lesson

we learn how to output the date author

name and categories for each blog post

and we also set things up so that the

category links to a categories archive

page and the author name links to an

author archives page but what we didn't

discuss in a previous lesson is how to

control the output of these archive

pages so WordPress is very intelligent

out-of-the-box it will use the index dot

PHP file from our theme folder to

generate all these different archive

pages on the fly but we can do WordPress

a favor and create a new file in our

theme folder named archived PHP and then

we can customize how these archive

screens behave so for example if we

click on this opinion link so this is an

archive for the opinion category

wouldn't it be nice if there was some

sort of header or title here that read

opinion archives and then when we click

on the author name wouldn't it be nice

if this said author archives colon and

then the author name so we're going to

learn how to do just that in this lesson

so let's get started our first step is

to head over to the code okay and we're

going to create a new file in the theme

folder named archive dot PHP now you can

copy and paste the contents from


into this newly created file and then

scroll to the top ok so we want to begin

customizing this file after this line of

code so I'm going to drop out of PHP and

then we can re-enter PHP here and then

the new code that we're going to add

will live right here where the cursor is

so we'll write heading level 2 sample

text close the heading level 2 so now if

we refresh we can see that when we're on

an archive so this is an author archive

screen our sample text is here and if we

go to a category archive screen it's

there but if we go to the home page it's

not there

so our archive dot PHP file is working

but we need to customize it and add a

bit of logic because we need to output

different text depending on what type of

archive we're viewing so for example

this is a category archive

this is an author archive if I click on

a post and we go look at the URL bar you

can see that this is a month archived

May of 2014 and this is a day archive

the 19th day of the fifth month of 2014

and even this is a year archived so

archive dot PHP is a very flexible

powerful file that can control many

different views of our posts so we need

to build conditional logic using if and

else if statements to output different

titles here depending on the type of

archive that we're viewing so let's get

started we obviously do not want sample

text so we'll delete that and instead

will drop into PHP and we'll build our

logic so the first thing we'll do is

check to see if we're in a category so

if we're in a category that means it's a

category archive and we can output

certain text here so we'll say for now

this is a category and we'll fill this

in with the proper text in just a moment

but that will do the job for now next up

what if it's not a category so we'll say

else if some other condition is met so

then we'll check to see if it's a tag

archive and then we can output certain

text that makes sense for a tag archive

there we'll fill that in a little bit

later in the lesson what if it's

something else

so we'll say else if what if we're in an

author archive then we can output text

that makes sense for an author screen or

an author archive screen so you can see

that all we're doing is repeating a

pattern we're saying if this condition

is met do this else if this else if this

and we're going to do this many more

times so we're covering all of our bases

now let's continue and cover the date

bases so we'll say else if what if it is

a day archive then we'll need to display

some text focused around a date for this

particular day now we'll fill the date

logic in later but for now we can just

say echo day archive what if it is a

month archive I will say that WordPress

makes this quite easy because WordPress

has all of these neat functions is

category is tagged as author is day as

month so we're not having to do the

heavy lifting and building these

conditional values that that report back

true or false WordPress is doing the

heavy lifting we're just building a

skeleton so we'll echo out month text

there in just a moment and what if it's

a year archive is year okay and then

finally if it's none of these if none of

these conditions are met then we'll say

else echo just simply archives so this

is the fallback line if none of these

conditions are met then we'll just

simply output archives let's be sure to

add a semicolon after this line and

let's go test this out in the web

browser so if we refresh we can see that

we're on a year archive and now the word

year is being output now obviously we

would want this to echo 2014 and not

literally the word year but this is just

a proof of concept to make sure that our

skeleton logic is in place so far so if

we go to a month archive you can see

that that's working if we go to the day


that is also working category archive

looks good and author archive looks good

so now our job is to build out these

echo lines for each condition and make

sure that the text were outputting

actually makes sense so for this first

one what we need to do is we actually

want to output the name of the category

now WordPress makes our lives very easy

in this situation all we need to do is

call a function named single cat title

yes it is that easy so now if we go to a

category archive we can see that the

word is in place its head back to our

code so the next scenario is if it is a

tag archive now I'm currently not

outputting the tag anywhere in the blog

post template but if we go to the URL

screen I'll show you that I do have a

sample tag set up named sample tag and

I've assigned it to two different posts

so let's go ahead and give this a shot

instead of echoing tag WordPress again

makes things simple with single tag

title function so if we refresh it's

that simple

in this case tags are very similar to

categories so moving on to the next case

which is if we're currently viewing an

author archive screen so we will delete

this echo author sample line and instead

we'll write this the post which will

sort of initialize the first post that

is being queried in this archive and

what that will do is make sure that this

following line will always work echo we

want to output the phrase author

archives colon space and then we want to

output the name of the author so get the

author now a quick note in some cases

this line of code will work without this

the post function but if your site has

more than one author and if the stars

are aligned just properly regarding the

way the posts are lined up on the

archive just include this because then

with 100% certainty this line will

always work perfectly now because we did

use the post which is sort of querying

the first post in the archive

we want to be sure to also run rewind

posts so that the loop can go on

unaffected so now if we refresh or we

don't even need to refresh if we just

click on an author to view an author

archives we can see that it's outputting

the exact text that we would like so

let's head back to our code and focus on

the is day scenario so we don't need

this sample text instead will output the

phrase daily archives colon space and

then we'll use a function named get the

date so now in our browser if we head to

a day archive so I will adjust the URL

to point to May 19th we can see that

daily archives colon and then the date

is being output now that one was simple

and we'll use something very similar for

the is month so we can just copy and

paste this line for this is month

scenario we'll change this to monthly

archives but then we want to include

some options within the parentheses for

the get to date function so instead of

leaving this empty will say output the

month capital F and then also the year

so if we refresh and if we go to a month

archive we can see that that is doing

exactly what we want and finally we'll

do something similar for the is your

scenario so we'll remove this paste in

our line change this to yearly and then

inside they get the date function all we

want to output is just the year now a

quick note if you're unfamiliar with PHP

date formatting there's different letter

codes that output different date related


so the digit for a month or you can

spell out the name of the month you can

output the year with four digits or with

two digits you have all sorts of

different options and different letters

represent different codes but if you're

curious go ahead and Google PHP date

formatting but for now if we go to a

year archive

we can see that it's working just like

we expected now this means that our

archive title section is complete we've

covered all of our bases now obviously

there's a lot more that you can do with

archivephp then just output different

titles so for example if on the archive

view we wanted to only output the

excerpt we could do that so we scroll

down we'd find the content and change it

to the excerpt so then we can see that

on archive pages only the excerpt is

being shown and if you click on the

actual post then you see the full post

and this means that the home page is

still outputting full post as well so

you can see the archived PHP lets us

control many different views for our

posts so this lesson was just to whet

your appetite and get you to begin

experimenting with the archived PHP file

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 Archive Tutorial (archive.php)”

Your email address will not be published.

There are no comments yet.