ad box 728 x 90
ad box 468 x 60
Hardware, Software, Code, PHP, ASP, Games, Reviews and Other Funky Stuff
Subscribe

Enter your email address:

Pic of the Month
Popular Posts

Posts Tagged ‘get the post id wordpress v cateogories’

WordPress how to get the first category name or id from post

I have been looking at how to extract the category names and other post information in a wordpress template.

It can be useful to be able to extract these items from each post so that we can base category images or just display the first category in the template. Using the following code i have been able to extract this information using wordpress in-built functions.

Here is how to extract the first category id from each post if used in the page loop:


$category = get_the_category();
$currentcat = $category[0]->cat_ID;

Here is one you can use out side of the loop:


global $post;
$categories = get_the_category($post->ID);
$currentcat = $category[0]->cat_ID;

Extracting the name of the category out side of the page loop.


global $post;
$categories = get_the_category($post->ID);
$currentcat = $category[0]->cat_name;