Feedback

Darren Hoyt Dot Com | Design, Development, CSS, CMS, WordPress

In Praise of Wordpress Template Tags

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”.

  1. 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:

    frontpage

  2. 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.

  3. 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 wait—the 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.

  4. 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.

  5. 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.

106 Responses {+}

Trackbacks

  1. oriolrius lifestream » Darren Hoyt Dot Com » Blog Archive » In Praise of Wordpress Template Tags
  2. Presseschau für Webentwickler - Ausgabe Juli 2007 | Dr. Web Weblog
  3. Best of May/June 2007
  4. lost node » Blog Archive » Best of May/June 2007
  5. Best of May/June 2007 | Webdesign (css, grafica e altro)
  6. Cyber Space in One Hand » Blog Archive » Useful references, tutorials, services, tools, techniques and articles by smashingmagazine
  7. Best of May/June 2007 · Style Grind
  8. Customizing Wordpress the Easy Way!
  9. Skylog » Blog Archive » links for 2007-07-26
  10. Best of May/June 2007 at Design Resources
  11. Mine del.icio.us-links den 30. juli
  12. 10 Links für ein besseres WordPress-Blog | NanoPub
  13. adventures of a blogjunkie » Blog Archive » links for 2007-08-03
  14. blogging
  15. » Wordpress Magazine Theme Released
  16. » Managing Wordpress Template Tags with Dreamweaver Snippets
  17. » Build A Dynamic Design Portfolio with Wordpress
  18. Länktips 26/9
  19. Darren Hoyt Dot Com » Blog Archive » Wordpress Magazine Theme Released
  20. josh fassbind » Blog Archive » Ladida, Ladida
  21. Thème en préparation : Mimbo 2.1 « Calyptratus’ WordPress Corner
  22. El portal de Angello Toro>>> » Portada » Post de la portada
  23. e l c r e e . c o m . a r » Lead Story » Cena de Egresados “Sociales 2007″
  24. UnAbridged Excerpts » Lead Story » Test Lead Story
  25. links for 2008-01-13 at The New Reader
  26. Oxygen » Archive du blog » Build A Dynamic Design Portfolio with Wordpress
  27. Burwood » LEAD STORY WordPress » Wordpress Magazine Theme Released
  28. vandervleuten.org » cover story » Mimbo Magazine
  29. Moniteur » Ecran Ecran2 Ecran3 » Kool
  30. rein:MEOW - 我要努力之把theme弄好
  31. » 我要努力之把theme弄好 | atdesign
  32. Blu Vintage » Lead Story » A New Day
  33. The Fresh Press » Destacados » Checking…
  34. Art Service Pfleging » Wordpress Themes » Mimbo Magazine Theme
  35. Photoshop Art » Blog Archive » aser
  36. FUZINE 2 » Téma » Téma d?a
  37. Hello World | Y e e e e e e
  38. Mimbo v2.2 - Wordpress Magazine Theme : Themes Blog’s
  39. for the life category | michael john grist
  40. reviews | michael john grist
  41. study | michael john grist
  42. » prova3 » tbrbtr
  43. Oltre il deserto » Senza categoria » rggg
  44. Oltre il deserto » prima » primo articolo
  45. Oltre il deserto » quinta » ultimo
  46. Reading Circle Books » » WordPress 2.5 Widgets — Taking the Load Off Your Mind
  47. The Magazine-Style Wordpress Theme Mimbo
  48. INTELLIGENT NAIVETY » The Entrepreneurial Museum » First entry on Entrepreneurial museum
  49. עמוד פתיחה לאתר » ITbananas
  50. nullvalue » Uncategorized » Metal Gear Solid 4 gets worldwide release, favourable reviews
  51. Thursday Links Roundup - Putting Blogs First
  52. Wordpress Magazine Theme Released | One Ummah
  53. WP Junkie - Mimbo Magazine Theme
  54. Art Service Pfleging » Blog Archive » Mimbo Magazine Theme
  55. FABULUSH » Lead Story » Testing
  56. FABULUSH » testing » First
  57. duane-francis.com » Test » Test
  58. My Blog » Features » About This Theme
  59. WordPress Blog » Feature Uncategorized » mambo
  60. Mimbo 2 - | Cwave Soft
  61. 6 Things You Didn’t Know WordPress Could Be - PuttingBlogsFirst
  62. Mimbo 2 - | Cwave Soft