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 one 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?
If you’d like to read some similar articles, take a look at Scriptastique, a blog all about web development and coding, with great tips on CSS, HTML, PHP, MySQL and Javasctipt and tutorials and screencasts coming soon! You can follow us on our RSS feed, or Twitter and Facebook!
Related posts:
Do more with your Wordpress using the function referenceWordpress Broken Link Checker
Wordpress: Your attempt to edit this post has failed
Wordpress Blogs: Create Custom Tag Pages
Wordpress SEO: Advanced Nofollow
List of Wordpress Plugins installed
Wordpress Plugins that I use
Subscribe To Categories, Authors Or Tags In Wordpress
4 Responses to “Wordpress template tags you should know”
Trackbacks/Pingbacks
-
[...] Wordpress template tags you should know If you run your own blog, you may want to make some modifications to it. Learning some basic Wordpress template tags is really easy, and will allow you to make changes and overhauls to your blog really quickly. [...]
-
[...] Wordpress template tags you should know (ghacks.net) [...]
-
[...] Wordpress template tags you should know (ghacks.net) Bookmark This Post [...]


Thanks. I found this to be a helpful introduction.