I’ve used WordPress for two years, first as a conventional front-page blog, then as a simple CMS, then both. As WordPress has evolved, the development team has done an excellent job providing documentation on how to bend and stretch the software’s functionality without requiring true PHP coding skills or unnecessary plug-ins. Ever since the release of v2.1, WordPress has become a much more viable CMS option, and Template Tags are one key to maximizing its potential.
Sample scenario: you’ve been tasked with building a site powered by WordPress, but the client doesn’t want the homepage oriented like a traditional full-blown blog with all the whistles. He wants instead a clean, minimal homepage whose only textual content is a chunk called “Latest News Excerpt”.
- Rearrange Your TemplatesThe first step is to assign the homepage its own template and stick the News section in a separate interior page where full-blown features like search, categories and comments can appear. How to do this? After building out the HTML and CSS for your homepage layout, save your markup to the relevant /themes/ folder and name it something like “homepage.php”. At the very top of the code, insert a snippet that looks like this:
<?php /* Template Name: Homepage */ ?>
Now log in to your WordPress control panel, go to Write->Page and create a page called “News”. Save that, then repeat the step, naming the next page “Home”. But before you save it, make sure to click the blue tab on the right called “Page Template” and select the “Homepage” item (which appears now as an option because of the header code we inserted above). You have now expanded your ability to distinguish separate templates for pages with differing layouts.
Your last step is to visit Options->Reading in your control panel and set your preferences accordingly:
- Insert Your Latest ExcerptOpen your homepage.php file and insert the following:
<?php query_posts('showposts=1'); ?> <?php while (have_posts()) : the_post(); ?> <?php the_excerpt(); ?> <a href="<?php the_permalink() ?>" rel="bookmark">Read More</a> | <a href="/news">All News</a> <?php endwhile; ?>You have now created a Loop which tells the software to query for new posts, grab the latest one, and print the first 55 words. I’ve added two optional links (“Read More”, “All News”) which can be seen on the homepage of DarrenHoyt.com. More info on the_excerpt function here.
- Print The Last Three HeadlinesOur client has now changed his mind about the homepage. Instead of an excerpt printed to the homepage, the client now wants a little newsy module which pulls just the last three headlines. Easily done, the code will look like this:
<?php query_posts('showposts=3'); ?> <ul> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul>But waitthe client doesn’t want headlines from just any category but from only the category called “Financial News”. In this case you will alter the parameter of your PHP function. First visit your control panel and note that the Financial News category is listed as ID# 10. So instead of “showposts=6″ you’ll change the code above to read “showposts=6&cat=10″. Parameters allow for a variety of customizations, and I’ll try to cover more of them in a follow-up post.
- Print a Lead Story Plus Three HeadlinesYour client’s wife looks at the site one night before it launches and decides the homepage should no longer be clean and simple, but should more resemble the New York Times where a lead story excerpt appears at the top and beside it are the last three “Financial News” headlines displayed in an unordered list:

No problem, your code will now read like this:<?php query_posts('showposts=1'); ?> <?php while (have_posts()) : the_post(); ?> <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a> <?php the_excerpt(); ?> <?php endwhile; ?> <?php query_posts('showposts=3&cat=10&offset=1'); ?> <ul> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul>The “offset=1″ parameter is included so the unordered list will pull the last three headlines except for the top headline, thus it won’t be printed twice.
- Display Third-Level NavigationDone with the homepage specs, your client has begun working on his “About Us” section and decided there will be three child pages within it. Lately he’s been admiring the Thomas Jefferson Center site (built on WordPress by what’s rumored to be some handsome Virginia designer) and how subpage navigation is listed dynamically in the left sidebar. First you create a unique WordPress template with the proper header code, and in the sidebar you insert:
<ul><?php if(wp_list_pages("title_li=&child_of=6")) { ?> <?php } ?></ul>The parameters in this example tell the PHP to display only the titles of child-pages in the section ID’d as #6 (“About Us”).
Your client now has a small-scale CMS armed with a searchable, categorized news database, aka, blog. I’ll try to follow up this post with a few more examples of useful template tags. Leave a comment if you’ve got suggestions or corrections.
4:16 pm
All bow down and worship the mighty WP! :)
You can also list subpages dynamically using this (much longer) code:
<?php /* Creates a menu for pages beneath the level of the current page */ if (is_page() and ($notfound != '1')) { $current_page = $post->ID; while($current_page) { $page_query = $wpdb->get_row("SELECT ID, post_title, post_status, post_parent FROM $wpdb->posts WHERE ID = '$current_page'"); $current_page = $page_query->post_parent; } $parent_id = $page_query->ID; $parent_title = $page_query->post_title; if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?> <ul> <?php $parentpermalink = get_permalink($parent_id); if ($parent_id == $post->ID) { echo "<li class='page_item current_page_item'><a href=\"$parentpermalink\">$parent_title</a></li>"; echo "\n"; } else { echo "<li class='page_item'><a href=\"$parentpermalink\">$parent_title</a></li>"; echo "\n"; } ?> <?php wp_list_pages('depth=2&sort_column=menu_order&title_li=&child_of='. $parent_id); ?> </ul> <?php } } ?>9:57 am
Excellent work, this is what I have always wanted to do with my site. I am using your theme. Looking forward to your new tutorial on making wordpress differently.
8:27 am
Thanks your post is delightful. I like your site.. bye
5:24 am
This is absolutely amazing!!! I’m going to try and build my site with this. Thanks a million, Darren! And keep up the wonderful work.
11:43 pm
Using static page in the main page is great and it makes the site look less blogging style. We managed to create a new theme, have a look at this theme called Banteay Sreywhich we created for charity purpose to help Cambodia Kids.
5:16 am
Nice Site!
2:22 am
Your site is interesting, I will visit every day.Thanks
3:03 pm
Your site is interesting, I will visit daily thanks for the info
11:29 am
Thankiossi
It’s great
2:20 am
Thankiosst
Great!
5:11 pm
Thankiossk
Cool!
7:05 am
thankiosso
Cool!
9:41 am
Having never developed a wordpress theme from scratch I decided to start learning and have set a project to start with my own site. I scanned through the codex pages reading about all manner of templates, custom fields etc and was completely overwhelmed. Your little tutorial in it’s simplicity has managed to teach me more in a short space of time than all the time spent on the codex pages. You should write an e-book, I’d buy it.
4:24 pm
Kompliment du hast sehr gute Informationen. Nicht nur diese Info, sondern allgemein finde ich deine News / Angaben sehr informativ.
6:52 am
Cool template, thx !
5:30 am
Thanks.. I find here what i have been searching via google for last 2 hrs. Thanks
1:45 pm
I am worried me for the good info and hope you will continue to write qualitative contributions.
3:47 am
I do think you are right on the spot with this post.
3:48 am
very nice blog
8:36 am
I use on my site, “Print The Last Three Headlines” change with Print The Last Ten Headlines :D
It looks very nice
10:00 pm
hi darren, first of all thank you for this great magazine wordpress theme mimbo. i am now trying to modify it but i am having problems.
i removed left column and put something like right column but instead showing latest photos from selected categories i’d like to show latest 2nd 3rd and 4th post at the left column and at right column i’d like to show latest 5th 6th and 7th post without looking at their categories, i mean just looking by publish date.
is it possible?
i am trying to find a solution but still couldn’t do anything. i think php’s the loop function must be used but i am not so good at php so i need your help.
i will be waiting for your reply.
thanks again.
5:49 pm
Thanks 4 Your info:) I think it will be usefull.
4:33 pm
Thanks for the informative article. It made me think up some things. :)
5:54 pm
news.healtharchive.net – Health News & Articles
3:20 am
4:09 am
9:08 am
5:10 pm
Hi Darren,
First of all i would like to say thank you for the extremely helpful information on this website, as someone who has only recently began looking into wordpress i haqve found it a vital read.
I have a quick question on the technique used above to display a lead story and 3 headlines. I have got this working on my Home Page however i cannot then get the remainder of the homepage to display. I tried copying some code over from the main page template but now it just shows the posts underneath instead of my home page text.
Am i missing something?
Thanks for your time,
Shaun
1:07 pm
Really informative article, it helped me a lot, had to come back a few times and readi it again. keep up the good work, thanks
4:54 pm
I was searching for this information when i landed up on your page. Thanks for the info.
9:18 am
It certainly has made my day to day blogging easier. Thanks for the post and additional information.
3:52 am
Nice tips..wordpress 2.5 great…easy to use
Cheers,
Online Issues
1:18 am
Hi all great information here and good thread to comment on.
Can I ask though – how did you get this picked up and into google news?
Very impressive that this blog is syndicated through Google and is it something that is just up to Google or you actively created?
Obviously this is a popular blog with great data so well done on your seo success..
5:53 pm
שיש,שישלמטבח,שיש למטבחי×
4:18 am
I found this website I’m a soccer player and love good products and a very important info… check it out !!!
warrm regards
joe
8:29 am
thats really awesome post really.
4:17 am
Your theme is very beautiful, and I would like to use it on my blog. But still I’m having some troubles with categories.
10:20 pm
Great work! Keep it up!
6:57 am
Hi, Thanks for this mimbo theme. I just Installed and now customizing for me.Once done will let you know my comments
1:29 am
thanks very much
5:27 pm
I tjink that WordPress is the best blog cms, despite a lot of gaps!
5:39 pm
Thanks for this article! This is very helpful!
9:19 pm
Excellent post, I am new to blogging but this will help me out alot.
12:30 pm
Great guide, thanks.