There have many concerns and question about the template system used in WordPress. Mostly the concerns are with n00bs and the complications of PHP. “I want to change this and that, but don't know PHP”. I hear this a lot. But the thing is, you don't need to know PHP to change the Templates in WordPress.
The Template System
Lets' start with “the Loop” as we call it. Probably the most confusing part of the Template for a non-programmer. The Loop looks like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//Template Section 1: post content Template Tags go here
<?php endwhile; else: ?>;
//Template Section2: no posts found stuff here.
<?php endif; ?>
It may look complicated, but it's actually quite simple. In Template Section 1, all the posts information will be displayed. Within that section we will put all the posts Template Tags, and our HTML to format them. In Template Section 2, we will display a message telling the user no posts were found, if we find none to meet their criteria.
Template Section 1: The Posts
We'll start with outputting the post tile. Simply add in the Template Tag <?php the_title() ?>. That will output the title of the post.
Now let's output the date of the post. Simply add the date's Template Tag <?php the_date() ?>. That outputs the date of the post.
We will also, of coarse, need the content of the actual post. Simply, again, add the Template Tag <?php the_content() ?>.
Now let's put it all together with some HTML:
<div class="post">
<h2> <?php the_title() ?> </h2>
<p> <?php the_date() ?> </p>
<?php the_content() ?>
</div>
And there's our simple template. Now let's add some more.
First let's add a link to the post, permalink as it's called, on the title. We get the actual URL, something like http://mysite.com/archives/2004/09/post-title/, with the Template Tag <?php the_permalink() ?>. We will need to put that in our <a href=””> HTML tag.
We also want to add in links to each page of our post, next page, previous page, etc.. We simply call another Template Tag, <?php link_pages('<p>','</p>') ?>. Now here's another confusion people have. We've added in, what programmers would call, arguments to our Template Tag. That is, we've added in '<p>', '</p>' to the brackets of the Template Tag. In this case we are just saying, if there are pages to link, output the page links with a <p> at the start, and a </p> at the end. Hence, enclosing it all in HTML paragraph tags. Most Template Tags have different arguments you can pass to them, causing them to format their output differently.
Now let's add in our new tags.
<div class=”post”>
<h2> <a href=”<?php the_permalink() ?>”> <?php the_title() ?> </a> </h2>
<p> <?php the_date() ?> </p>
<?php the_content() ?>
<?php link_pages('<p>','</p>') ?>
</div>
Now we have a fully functional Posts section, Template Section 1. Let's move on to Template Section 2.
Template Section 2: No Posts Found
For our Template Section 2, the no post found section, all we need to do is put in a message to tell the user nothing was found, to meet their criteria. Whether it was a search or a mis-typed URL. So let's use the following simple HTML:
<p>Sorry, no posts could be found to match your criteria.</p>
Now let's put it all together.
Complete Template
Putting all we learned above together, we get a nice simple Loop Template.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class=”post”>
<h2> <a href=”<?php the_permalink() ?>”> <?php the_title() ?> </a> </h2>
<p> <?php the_date() ?> </p>
<?php the_content() ?>
<?php link_pages('<p>','</p>') ?>
</div>
<?php endwhile; else: ?>
<p>Sorry, no posts could be found to match your criteria.</p>
<?php endif; ?>
As you can see, we have done no PHP coding, just some simple HTML with WordPress Template Tags added in.
Obviously this is not a complete template, as we have no header and footer containing all the <html><head><body> tags. But with some simple HTML and more Template Tags, and no PHP, we can easily add all that is needed for a complete HTML document. That's right, an HTML document, not a PHP script.
As we can see WordPress is not PHP. WordPress is powered by PHP, but uses a simple template system, easily modified by even the most novice of users. None of that complicated Perl code like MT has ... Yes, believe it or not, MT is also powered by a complicated programming language. The only difference is marketing. MT has been marketed as having simple templates with no programming skills needed. However, somehow, and I don't know why, WordPress has gotten a rap of being complicated and only for the hardcore programmers out there. But as we can see, using the Template Tags is extremely easy and requires no knowledge of PHP at all.
Changing the format and style of your Template is as easy as using a little HTML, CSS and Template Tags. Just like other Weblog systems out there.
However, as with any software, there is room for improvement.
Improving Templates
One of the problems with the WordPress Template System, in my opinion, is the default Template, or theme, itself. Specifically the sidebar, with all the is_*() functions. We shouldn't expect a novice WordPress user to understand what these functions mean. I would suggest removing all the if(is_*()) statements in place of a simple sidebar with just the essentials. Say, Search, Pages, Archives, Categories, Links, Meta information and take out all the complicated if statements and includes.
Another problem I see with the default Template is the CSS in the header. I think all CSS should be contained in the style sheets themselves. Again remove the if statements. When a user wants to modify the CSS it seems logical to have just the simple CSS in one place, to easily modify.
Another problem I see is with some of the Template Tags. Such as <?php bloginfo() ?>. Instead of having one function to display some necessary information, like blog name, description, url, etc., why not have specific tags for the highly used information. Like say, <?php blog_name() ?>, <?php blog_description() ?>, <?php blog_url() ?>, etc., to maintain consistency. For some of the less used information, charset, version, etc., the <?php bloginfo() ?> tag would be fine.
And some of the other advanced Template Tags in use in the default Templates, like <?php list_cats(0, '', 'name', 'asc', '', 1, 0, 1, 1, 1, 1, 0,'','','','','') ?>, should be simplified to not contain all those arguments. Maybe use <?php wp_list_cats() ?> where all those settings would be set in the admin area.
It would also be nice to try to simplify the loop, although I can't see any way of doing that now, to use as little actual PHP, like if else statements, as possible. Also take out the else, for when no posts are found, and always use the 404 template file.
The point I'm trying making is to take the PHP out of the Templates, as much as possible, so users don't get confused and think they need PHP skills to modify the Templates.
Those are just my thoughts on the matter.
Update:
Stuff I write doesn't usually get this much attention so I thought I should clarify some things, as I usually just write what's on my mind and do little to no editing afterwards.
The intentions of this article was not insult anyones intelligence by making something that does take time to learn, seem so simple a monkey could do it ( I like Monkeys ). I was just trying to show that the templates can be easy if you think about it the right way and not get stuck on all the PHP. ( and please be nice to the monkey, he's very nice and doesn't usually bite )
Update 2: If you need any help with your templates, or would like to learn more about the monkey, I'd be glad to help out. Just drop me a line.
Kyle J Tomka on (#24171)
Matt,
Good Post. A lot of information some of which I put to use matching my companie's website (http://www.doclanding.com) to our blog (http://blog.doclanding.com). Thanks!
Mark J on (#1831)
Mega, mega kudos. Required reading for people new to WordPress.
Robert Deaton on (#1832)
Its about time someone posted something like this. If you ask me, theme developers need to stop their bitching and sit down for a minute and look at what they're doing before saying "ohgodidontknowhtisicandoitbetterstartbitching"
Erik on (#1833)
Just wanted to say thank you!
That sweet tutorial really helped me start working on my wordpress site.
Thanks!
Gorgeous site by the way...
on (#1835)
M.P.R.D
Roger Benningfield on (#1836)
I dunno... I think you're greatly underestimating how intimidating this stuff is.
Those aren't "template tags", after all; they're function calls with pointy brackets around them, and thus bear only the most trivial resemblance to something more familiar, like HTML.
Now, I'll grant you, if you've got a user who doesn't even grasp HTML, WP's templates may be no more complicated that anything else. But is such a user going to monkeying with her templates in the first place?
Matt Read on (#1837)
Roger, you're right, technically there are function calls. But technically a stop sign is a piece of sheet metal with paint on it, yet it's a stop sign. In this case those function calls are Template Tags.
If we look at any template system, technically all the template tags are function calls. They just get replaced with the content using some regex. So whether we're using
{$the\_title}or<?php the\_title() ?>, they are both template tags. The fact that the later is actual PHP is simply superficial. It's a template tag that just looks different than others, but effectively is the same.I'll I'm saying is that let's stop thinking of it as PHP, and start thinking in terms of templates. It's just an HTML document with some WP tags in it. There is no PHP coding required. Thinking like that, and helping people out by using generic "template tag" terminology, instead of using programming terminology, will help reduce the amount intimidation.
BoBB on (#1838)
Awesoem write up Matt, This would have been incredibly useful when I started using wordpress because I just could not figure out how it mixed in the content with the formatting. Its come a loooong way since then
Indranil on (#1840)
Bravo, really well explained.
You've done a great job.
Florian Jung on (#1841)
Remember that PHP was originally designed as a template system for use with HTML. Only later it advanced to a full blown programming language.
What I like in WP is that you can start in a very simple way, only using some of the template tags, but if you really need it, you can use the full power of PHP to do some magic without learning another easy (but also proprietary) language.
Jeff on (#1842)
Matt,
I think your dismissal of MT templates' perceived ease-of-use as pure "marketing" smoke and mirrors is incorrect. MT indeed has a full separate and well-documented template "language". An MT user could go his entire life, creating and customizing templates, without ever encountering Perl or even knowing Perl is under the hood.
Yes, MT templates have a syntax of their own that may need to be picked up, but it's like HTML: angle brackets and attributes. (example: ) Not hard for HTML authors to see the similarity between this and html tags. And that's why it's easier to learn.
As mentioned earlier, the WordPress template tags are function calls. For a novice user who's never programmed before, the syntax is completely alien. Open and close parens, commas between arguments, what an 'argument' even is, if statements.
Ozh on (#1843)
Nice article, I'll keep it in mind to redirect here n00bies asking for support about templates.
Time permitting you could extend it to a more complete series of tutorials, don't you think ? :)
on (#1844)
Thank you Matt, i have a question about the navigation system in your site, how did you assigned an id for archives link? i tried to do that in wordpress but i don't know how, so i hope you can write something about it.
And sorry for my poor english
befuzled on (#1849)
I suppose I am one of the n00bs, you speak of. Learned html then CSS several years ago. I know enough to have all my pages validate properly, yet have no ability explain what part of the language is what (ex: selector, element, property etc.).. I just know how to make it work. ..So I decide to use WP.. Oi vey, find it very confusing to change the template. Those "template tags" are making my head swim! I appreciate your suggetion to think of them as HTMl with WP Temp Tags... After many searchs to attempt to understand how to change my layouts, this has been the most benifitial. Still struggling to understand what I have to do to make it all work, but your words have at least given me the confidence that I might yet still be able to "get it".
Dougal Campbell on (#1850)
If you haven't already, how about throwing this little tutorial up on the Codex?
rob on (#1853)
Meh, I still think that WP should use something like Smarty. Separating template logic from programming logic is much nicer.
Andy on (#1854)
Great work Matt, this will be very beneficial to lot's of new users.
Mark on (#1861)
If WordPress Templates are not PHP, then why use the confusing
<?php ... ?>tag convention?Surely
<? the_title(); ?>would be less confusing for new users.Jemima on (#1862)
My biggest problem with Wordpress when I installed it was the insane range of places from which to get information about doing the most simple things with the templates. I'll follow instructions and use a template system, regardless of syntax, but please please please don't make me check pages and pages of Codex, all named for people who know what they're looking for, a support forum, some old documentation, a plugin library, another plugin library, lots of individual homepages about plugins, articles like this... All just to work out how to get something that may or may not require a plugin but comes out of the box in MT. And none of the documents in these places consistently say which version of WP they refer to, so you can spend ages barking up the wrong tree. At least with MT, which I used to use, there's one page for how templates work. Full stop. As it happens, I know php and am committed to the idea of open source and a robust architecture, but the frustration of the documentation really made me think twice about it. And now the disclaimer: I like WP, I use WP, I gripe because I care.
the absent student on (#1863)
This is great, and I wholeheartedly support all your suggestions for simplifying the template system further. I'd add that given the amount of PHP in the default template, the 'classic' theme is nearly always a better model for people wanting to build their own templates; it may not be as full-featured, but that makes it a lot easier to reverse-engineer.
Shayne on (#1866)
Well, it still is PHP. I understand what you are doing. But, honestly, I would have chosen a different title. Maybe, something that's more like, "you don't need to know PHP to modify a Wordpress template."
Hrm ... any thought?
on (#1829)
f
Matt on (#1872)
Mark, the reason we don't use the short opening tags (without the ?php) is that they don't work as reliably cross-platform
Matt Read on (#1875)
Jeff, I didn't mean to say MT template were not easy, cause they are very easy. I was just pointing out that eventhough the syntax is, as you say, alien to some, it is still easy to use.
Jemima, I agree with you, the documentation is all over the place right now. But there are many great people working on making that better.
Shayne, That would prolly have been a better title, but I like the sound of mine better.
Anonymous, the navigation is using PhotoMatt's Intelligent Menus Script. It's quite lovely.
jerome on (#1879)
I've supported several people trying to make sense of their templates and I have to disagree with you: WordPress "template tags" are PHP functions, plain and simple.
The two biggest causes of frustration:
1. passing arguments to WP functions requires knowledge of PHP. Do you use single quotes? Double quotes? Include the parameter name? The average HTML/CSS coder isn't going to know this right off the bat.
2. too many WP functions output their own HTML tags, leaving users struggling to understand what the end result will look like.
As a final note, you really shouldn't start off your article referring to new users as "n00bs" -- it's demeaning and deters your target audience.
b on (#1884)
i think its still PHP!
Matt Read on (#1885)
Jerome,
1. You're right they are PHP functions, but they are used as template tags. It does take time to learn, but it's not PHP. Well, technically it is, but it's also the way the WP Template Tags work. So I would say it's not learning PHP, it's learning the WP Template syntax. That's why I say it's not PHP.
2. I agree completely. They can cause problems in outputting their own HTML. But most info they output is semantically proper. Not all, but most.
"n00bs" I don't see as demeaning. I know I'm certainly a n00b in many many things. But yes, some may find it demeaning, bad choice on my part. Thanks for the input.
b, I don't.
James on (#1888)
Is there a "complete" manual for Wordpress"? I read tutotial here and ther. I could not find a more systematic "howto". Am I missing something?
Jauhari on (#1896)
This Tutorial so Incridible for me, I learn something new from this Post
Thanks
Root on (#1897)
This is awqesome. Beautifully written, very stylishly presented, and putting really useful but technical info in front of the end user in an accessible way. You have a gift my friend. The real joy is that you have articulated clearly what many of us in our stuttering way have been trying to say. For that I thank you. It is not surprising that all the usual suspects
are here in a supportive way. You deserve it.
Dante on (#1902)
Great article, terrible title. I suggest changing the title to "WordPress is not Programming".
Petros Dimitriadis on (#1929)
this article is quite useful as well http://www.urbangiraffe.com/category/software/wordpress/
Denis de Bernardy on (#1944)
WordPress is way too complicated for the average user. I tend to second Jerome's comment... No user in his right mind should ever need to configure a function to customize his WordPress install.
Consider these insights on improving WordPress' usability.
Ron Pemberton on (#1969)
Please write more tutorials. You have a talent for it, and I would gratfully appreciate someone with the moxy to do it with panache. Please pickup where Podz left off, with your presentation skills ou would most definately become famous for it. Thank you for your help. Kudos!!!!
HAMZA on (#1971)
Car Loans on (#1973)
It is nice to see such an active website with a wide range of topics and opinions. Makes for some good reading.
New and Used Car Loans nationwide.
Fish Cakes on (#1989)
Goddammit, this is exactly what I needed. The way people talk about (insert scary voice here) the loop makes it seem like some kind of monster that lives in a lair. But when you break it down like that, it seems much more simple. Thanks, at least now I partly understand what powers my blog!
Dgold on (#2057)
Thanks for the write-up. Now if you could explain how CSS fits in, using equally simple terms and clear color-coded examples, I'd be most appreciative! I've figured out how to move and place "chunks" of data on my site inside HTML (these template tags), and even how to write the arguments properly (whoo hoo I guess I did some PHP). But now I've got CSS div span class scattered around, that I don't know where to start, or even what program to open it with for making changes (Notepad? Firefox extension?).
Tim on (#2060)
I thought it was PHP till I really started editing some of the actual php files that run WP. It was then I realized what PHP was. codex.wordpress.org is key. Thank you Matt R. for this post.
will the brand noobian on (#2065)
Please put this on the codex. I've been trying understand that for 2 days. 2 long days. Great article. thanks.
Is there a model anywhere? what connects to what... what the hell are all those folders...
Andr on (#2066)
FINE! I’ll just delete your sig. boooo!
Quy on (#2070)
It seems that you know alot about wordpress. What is the php line of code that makes list. I want to put links to other site on my sidebar however I don't know the code to make the list expand. I have seem them in other wordpress sites.
Please email me back a.s.a.p.
Thank You,
Quy
Valery on (#2157)
Very straight to the point and it answered my doubts!!
messett on (#2166)
Matt,
Thanks for the article. I wish I would have found it sooner. After countless hours of struggling to make sense out of all this I stumbled across your blog.
Definitely required reading for anyone wanting to use WordPress.
Thanks again,
Bill
SenorKaffee on (#2191)
Phew, this article encouraged me to get going. All I want to do is using the blogging functionality of WP as a better news script on small webpage of otherwise static pages. After all that confusion it looks like it is actually not hard to do it. Just design the webpage in Dreamweaver as always and add the WP tags where they are needed. No need to implement the whole site navigation and static sites in WP. :mrgreen:
Neebone on (#2206)
People are getting hung up on semantics. Anything can be construed as anything. Ok we get that it's actually PHP but it's PHP templates so we can just say templates. And like Matt said, it's easier for beginners to understand that way.
I don't really like the lack of docs on the themes that come with WP. I can get around on my own but I dont really have that much time and would rather read a code map or something.
Useful article though...well done.
Neebone
Louis on (#2247)
I couldn't figure out how to install MT and I gave up before even trying very much. I tried Wordpress and I was hooked. It keeps getting better all the time. This tutorial is great. I have been monkeying around with template tags, for me it is fun to do. On the codex you can find all of the template tags, you can do copy and paste and just change the variables and you are set, in a way it is easier than doing html.
Thanks for helping me see things in a little easier light.
Ivan Minic on (#2253)
:mrgreen: very nicely written... will give it a try
r.boylan on (#2265)
I ran across The Holy Grail of Layout over at http://www.alistapart.com/articles/holygrail and want to use it to create a WordPress theme. I can handle the CSS but I'm really frustrated by the lack of a foundation of template pages to begin the customization. I can see that starting with the Default theme or the Classic theme will eventually lead to what I want but my question is: Isn't there a set of foundation pages for developing a new theme? The Default theme is a double column within a single column and the Classic theme is the same. At many of the CSS sample sites, there are one column layouts, two column layouts, etc. Is there anything like that for WordPress? If so, where? Thanks, by the way, this was a marvelous article! (I have used PHP for some of my web sites so it does not scare me but your theme of it is NOT PHP was intriguing and you point was VERY well made.)
Robert on (#2270)
Great article. Thank you very much.
Natali on (#2295)
Thanks so much for the good advice !
USTommyMC on (#2302)
I don't know who's intelligence got insulted by this awesome tutorial but I think you did a great job. The WordPress Codex is next to useless when it comes to us "noobee" or whatever it is you people call us.
I understand "The Loop" a hell of a lot better now after reading this than I ever did reading the Codex over and over and over. Thank you for taking the time to put this together.
on (#2310)
:P
Alan on (#2311)
Hi dude,
I really wish to thank you for your knowledge and how you explain very well all the things...
I'm quite new in php, I'm a dj but for my site I use WD.
The problem is that I wish to modify my template, split the design,
or for explain better have more pages into one page.
Btw, your explain to how work WD was very useful and interesting and I'm writing to you
to thank you for your great work.
Best wishes and greetz...
Alan
on (#2315)
Thanks man, you just helped me to integrate worpress in my website layout.
htdjm on (#2348)
kan on (#2424)
Immobilien mieten kaufen inserieren on (#2453)
nice plugin, thanks for work, more plugins for wordpress here : Word press and here P-Lugins
and custom themes here: Wordpress themes download
Someone who got the same message. on (#2455)
Comment 57 appears may be social engineering spam.
http://www.google.com/search?q=%22nice+plugin,+thanks+for+work,+more+plugins+for+wordpress+here%22&num=100&hl=en&lr=&safe=off&filter=0
Hi gais on (#2467)
http://www.bochka.us/stevenson-silverado-649/index.html Hello, it's very good site too: www.bochka.us/stevenson-silverado-649/index.html
Ivan Minic on (#2470)
I get this stuff all the time.. almost every serious sofware has similar template system and all people look is extension..
Sweet on (#2477)
Hello and congratulations! nokia6630
Mr Green on (#2497)
Hi Matt, regarding your update 2 =)
I was trying to override my default admin setting -> 2 posts displayed per page for the archive (by author) template.
So I tried the get_posts command. The loop I tried does not work, the post_author=$curauth->ID is not working. Could you tell me what I did wrong please?
ID');
foreach($posts as $post) :
?>
" rel="bookmark" title="Permanent Link: ">
,
in
Mr Green on (#2498)
um, sorry.. the code I posted turn out wrong :} here:
ID');
foreach($posts as $post) :
?>
" rel="bookmark" title="Permanent Link: ">
,
in
End Loop -->
Mr Green on (#2499)
sigh... please visit,
http://paste.uni.cc/8669
for the code I used =)
Mr Green on (#2500)
In case you would like to see the entire template:
http://paste.uni.cc/8670
on (#2506)
idiot
on (#2507)
for($i = 1; $i
on (#2508)
l
fsd on (#2529)
dsfsd
Clair on (#2633)
I need some help with templates in Pages. I have been using WP as a CMS and would like particular pages and their children/child pages to use certain templates. The thing is that I'd like to find a way to set the templates at one go and not set them for each page. Is that possible? How? Thanks
j4s0n on (#2636)
I don't understand. Wordpress is PHP and will be, and it's just great to have the templates in PHP compare to other CMS/Blogs around there. I aint catching this.
Dotyyw on (#2643)
Thank you!!! I'm impressed! Really useful! [URL=http://www.mp3extramusic.com/v.htm] heaven mp3 download [/URL]
Azzurra on (#2702)
Buon luogo, congratulazioni, il mio amico!
Sindhu on (#2729)
ur wierd man!
a n00bie wouldnt know what a loop meant. for all people wanting to mess around with theme n php, youve to have some idea about programming , say if u know C , its better. i struggled without C, once i knew, i couldnt easily figure out stuff,
but u dont neccessarily have to know C, if you do then thats great.
kirt on (#30866)
Okay, so if it's not php then wordpress developers won't bother using any of these php syntax. I don't really get your point! Your tutorial is just simply explaining how the template works. I do not know if you are a php coder or a php programmer. I do not know where did you get this idea writing this such tutorial. You are just misleading people here.
It would be much better giving this tutorial a different post title rather than saying wordpress is not php.
Well i guess you should dig up more into wordpress files for you to know that wordpress is php.
Don't you get it? If it is not php then don't bother to install xampp into your local machine to keep wordpress running, dont bother to use and other php syntax into wordpress if it is not php don't name wordpress files with .php if it is not php at all and forget about php.
This tutorial is a waste of time. It doesn't make any sense at all.
Matt Read on (#30868)
Kirt, I'm not sure if you can see beyond what's directly in front of you, but your comment makes no sense, and is a waste of time. Thanks for your contribution.
Anyway the title of the post goes way back to previous arguments within the wordpress community. And I don't care anymore. kthxbai.
Linux Server Blog on (#30876)
Hi Matt,
I'm starting to look into theme design,
and your post here is the first im reading that explained so clearly how the loop works.
I finally heard the coin dropping..
thanks !
Yonit
Dubcomm on (#31099)
Awesome help, this is a great breakdown of the loop functions... one that even I could understand! Thanks very much!
Jake on (#33018)
Hi Matt,
I found your article helpful in gaining some basic understanding to what wordpress is doing. I know a little bit of php, enough to be dangerous but not enough to fully understand wordpress' functioning.
I have been having trouble getting some of my own php code to interact with wordpress. I run an html form with a php file as the action. Unfortunately, once I run the php, I'm out of the wordpress environment. I would like to know if there is a relatively simple way to use my own form, run some php based on user input, and then display results ala wordpress. Do you know of any examples like that?
I've also read the wordpress site about creating templates and it indicates that you have to "assign the templatename as the Template when creating the action Page." I'm not sure I understand what is involved in this "assignment." Where do you do it? How?
Thanks for your help,
Jake