The 3 Least-Known, Incredibly-Helpful Wordpress Category Functions

We give you Interactive Know-How

While Wordpress is an incredibly great CMS/Blog tool in the arsenal here at Fourth Floor Interactive, our true power lies within our knowledge of the PHP language. This experience and knowledge allows us to further understand and manipulate Wordpress’s data capabilities to provide our clients with exactly what they need from Wordpress as their CMS/Blog tool.

While Wordpress is technically an open source web application, making it essentially ‘free’, these types of web applications also come with their share of set-backs.  In Wordpress (and other open source web applications), there is often little or spotty documentation in regards to manipulating the data provided with Wordpress-defined functions.   We’ve come to find that many of these functions that are built-into Wordpress are not always documented within their site, especially when it comes to functions that deal with Wordpress categories.  For this reason, we’ve decided to share some of our findings:

get_query_var(‘cat’)

This function can be used within category.php, single.php, etc.  It will return the current category ID if there is only one category assigned to the post or will return an array of categories if there are more than one.

Code Example:

$fourthfloor_CurrentCategories = get_query_var('cat');

To retrieve the ID from each category assigned to the post, reference it within a for loop (or foreach if you so choose) as the code example below:

for($x=0;$x<sizeof($fourthfloor_CurrentCategories);$x++){
   print $fourthfloor_CurrentCategories[$x]->cat_ID;
}

get_cat_name($my_category_id)

This function can be used within category.php, single.php, etc.  It will return a category name (this is the name, not the nicename/slugname) when you pass a valid category id (numerical value) to it.

Code Example:

$fourthfloor_CategoryName = get_cat_name(5);

get_cat_id($my_category_slugname)

This function can be used within category.php, single.php, etc.  It will return a category id (numerical value) when you pass a valid category slugname/nicename (this is the nicename/slugname, not the name) to it.

Code Example:

$fourthfloor_CategoryID = get_cat_id('fourth-floor');

Tags: , , , , ,

Leave a Reply