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.
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?Advertisement