Useful WordPress Template Tags for Theme Development

by Kawsar Ali

on June 23, 2010

in Tutorials

Template tags can be used in your wordpress theme to display information dynamically or retrieving information about your wordpress blog. In this post we will show you some easy and useful wordpress template tag that you can use in your theme.

Template tags are codes that instructs wordpress “get” something/information for example to getting your wordpress blog description, title, category. The great part of using template tags is you can avoid using plugins, which can certainly make your website faster. Before we get started you should have the basic knoweldge of PHP, since wordpress is built on it. Also, just to make sure you must wrap all with codes with <?php (all codes here) ?> .

Listing WordPress Categories

This tag is used to retrieve all the categories in your blog. You can also pass different parameters to exclude some categories or other functions. The simplest way to pull your category is :
[cc escaped="true"]
wp_list_categories(”);

[/cc]
The result you will get it is

Lets, pass some parameters to make it more useful and clean.
[cc escaped="true" ]
wp_list_categories(‘orderby=name&title_li=’ . __(‘Categories’) . ” );
[/cc]
This tell wordpress to pull up the categories, order them by name, put the in a ordered list with a h3 title tag. You join parameters or arguments by using the “&” between them;

You can also customize the look by using the CSS. Usually default CSS
for wordpress categories is “categories”. So you would style it by
[cc lang="css"]
li.categories{
list-style-type:none;
}
li.categories a{
color:#999;
text-decoration:none
}
[/cc]
If you wanted to include or exclude categories or some other modifications you can do so by following codes
[cc escaped="true"]
/* For including certain categories to the list */
wp_list_categories(‘orderby=name&include=2,9,11′);
/* For excluding certain categories from the list */
wp_list_categories(‘orderby=name&include=3,4′);
/* Will show rss links right next to the category*/
wp_list_categories(‘orderby=name&show_count=1&feed=RSS’);
/* Recently updated category and showing how many post on that category*/
wp_list_categories(‘show_last_update=1&show_count=1′ );
[/cc]

Further Reading

Getting the Tag Cloud

Showing wordpress cloud tag is very simple. Just specify the size of the most and least tagged post and how do you want to order it by.
[cc escaped="true" ]
wp_tag_cloud(‘number=30&smallest=14&largest=30&orderby=name’);
[/cc]

This code displays 30 most tagged post, the least tagged font size is 14px and the most tagged font size is 30px and they are ordered by name.

Here are some other parameters you can use:
[cc escaped="true"]
/* Display your category as tag cloud */
wp_tag_cloud( array( ‘taxonomy’ => ‘category’ ) )
/* For including excluding tags (the tags with that term_id0 from the cloud */
wp_tag_cloud(‘smallest=14&largest=30&orderby=name&exclude=6,24′);
/* Display your tag cloud as list */
wp_tag_cloud(‘format=list&smallest=15&largest=40′ );
[/cc]

Further Reading

List WordPress Pages

Listing how many pages you have is pretty much same as category. Therefore, I won’t go into too much details

[cc escaped="true"]

/* Listing all pages*/
wp_list_pages(‘title_li=’);
/* Exclude pages */
wp_list_pages(‘exclude=17,38′ );
/* Order by date */
wp_list_pages(‘sort_column=post_date&show_date=created’);
[/cc]

Further Reading

List Popular Post Without Plugin

This method actually uses  wpdb class function. As Bin-Blog shows points out here , an easy way to list the popular post is by using wpdb class function to get the result for most commented posts.
[cc escaped="true"]
$popular_posts = $wpdb->get_results(“SELECT id,post_title FROM {$wpdb->prefix}posts ORDER BY comment_count DESC LIMIT 0,10″);
foreach($popular_posts as $post) {
print “<li><a href=’”. get_permalink($post->id) .”‘>”.$post->post_title.”</a></li>\n”;
}

[/cc]
This code with gives the following by result:

The only problem with this technique it does not specify post or page. It gives you result for both pages and post. If you look at the above screenshot the Theme is a page. So, how do we get around it. A simple solution : using mysql query to specify post_type in wordpress database  like this: WHERE post_type = ‘post’. This will only output most commented post.
Here is the modified version:
[cc escaped="true"]

$popular_posts = $wpdb->get_results(“SELECT id, post_title FROM {$wpdb->prefix}posts WHERE post_type = ‘post’ ORDER BY comment_count DESC LIMIT 0,10″);

foreach($popular_posts as $post) {
print “<li><a href=’”. get_permalink($post->id) .”‘>”.$post->post_title.”</a></li>\n”;
}
[/cc]

Get the Recently Published Post

WordPress  get_post template post is very useful tag. Many useful information can be retrieved by using the tag. Again we are going use  wordpress mysql database to inquire the latest post and print them in a list .

[cc escaped="true"]

$postslist = get_posts(‘numberposts=10&orderby=post_date’);
echo”<h3> Recently Pubished</h3>”;
foreach(  $postslist as $post ) {echo “<li><a href=’”. get_permalink($post->id) .”‘>”.$post->post_title.”</a></li>\n”;}

[/cc]

$postlist variable gets the latest 10 posts and order them by the recent date by using post_date table. We have added an echo for the title ( 2nd line). You can customize it however you want. Here is the result you should get:

Customizing WordPress Archives

Many websites like to customize their archives differently such as as by month, by year and etc. This allows the reader to browse however they want. That’s where wp_get_archives tag comes in. We will show you couple of ways you can list your archives:

List all the post alphabetically

[cc escaped="true"]

wp_get_archives(‘type=alpha’);

[/cc]

List last twelve month post and Show the amount of post each month

[cc escaped="true"]
wp_get_archives(‘type=monthly&limit=12&show_post_count’);

[/cc]

If you want to organize your wordpress archive into columns check out Jeff Star’s post. Jeff explains in details how to do it.

Further Reading

That’s it for now. You should get familiar with all other wordpress templates tags if you are looking to get into wordpress theme development or customizing your own theme.

Powered by Shutterstock

About Kawsar Ali