Tips of WordPress Categories

November 18, 2008 · Print This Article

Outside the common use of category tags in WordPress templates, there are very useful functions that can be very powerful and can help you achieve many different things with categories.

Tip 1: Get Category name by its ID
You could use the following snippet to get category name by its ID.

<?php
$catname = get_cat_name(1);
print $catname; // Would print the category name for category ID 1
?>

Tip 2: Get Category ID by its name
You could use something like this.

<?php
$catid = get_cat_ID('Videos');
echo $catid; // prints category ID for Videos
?>

This can be very useful If you wish to display category links non-traditional way. How?

<?php
$id = get_cat_ID('Videos'); // return category ID for videos category
$link = get_category_link($id); // asks wordpress to store the category link for found category

// When you are ready to display the category link
print '<a href="'. $link .'">Videos</a>';
?>

The above simple script will display a clickable link to Videos category and you do not have to use it in the loop. This is just simple and short.


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.