WordPress template tags you should know - gHacks Tech News

WordPress template tags you should know

If you run a WordPress blog, you will eventually want to make some modifications to your theme.

Perhaps you want to show your tags, not just categories, perhaps you want the date in a different format, you might want to add the URL of the author to each post, there are a lot of things like that which can easily be done with a little knowledge of WordPress template tags, which are little PHP code snippets, but you don't have to be a coder to use them.

The great thing about these tags is that they are very well documented "see link above", and that they are extremely simple to use. You don't need to know anything about PHP, so let me explain their basic use in common sense language. WordPress uses a loop, elegantly called "the loop" in "Wordpressian", which cycles through the posts you have.

If you show 10 posts on your main page, the loop cycles through the latest 10 posts. This means that on the first loop it will pull in the data of your latest post, on the second pass it will look at the second and so on. All the data is pulled, all you need to do is specify what you want to display out of that data, let's take a look at how.

wordpress template tags

First of all, you need to identify the start and the end of the loop. The loop starts with the following (there may be some variation) :

<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>

And ends with a "<?php endwhile; ?> <?php endif; ?>". This end statement is usually followed by the page navigation links which let you "turn the page" to the next or previous posts. Anything inside the loop gets executed as many times as the number of posts which are shown.

If all you want is to show your title, you just need to put "<?php the_title() ?>", and you will get the titles of your first 10 posts. If you also want to show the tags you can add "<?php the_tags() ?>". There are many others you can add, and don't forget, you also need some HTML and CSS to make them look good.

Here's a list of the most basic ones and what they do, but you can see the full list and expanded usage by clicking on the link above. Remember that the following should be put in the form: <?php function() ?>

  • the_title() - outputs the title of the
  • the_content() - displays the actual post body
  • the_category() - displays the categories the post is in
  • the_tags() - displays the tags for the post
  • the_author - outputs the post author's name
  • the_author_url - outputs the link to the author's page, used a lot in links with "the_author"
  • the_time() - outputs the time of the post, you need to add the format in the parenthesis
  • the_permalink - outputs the posts permalink, usually used in a link with "the_title"

While I use many others now and again, there are loads of designs that only use these 8, you can do a lot, with these alone. As you can see, adding to and modifying a WordPress template is not such a big deal, why not try it yourself?

Summary
WordPress template tags you should know
Article Name
WordPress template tags you should know
Description
The tutorial lists important WordPress template tags that webmasters who run WordPress blogs and developers should know about.
Author
Publisher
Ghacks Technology Nwws
Logo

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:

Comments

  1. Tysen Woodlock said on March 30, 2009 at 1:15 pm
    Reply

    Thanks. I found this to be a helpful introduction.

  2. Praveen said on April 18, 2010 at 8:11 am
    Reply

    You could add some pictures in ur article…..It will be very useful for newbies…..

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.