Detect Category ID using the Category Slug in WordPress

January 28th, 2010 By WP Tricks Posted in Tricks

Sometime WordPress Developer need to know Category ID, I will show you how to find Category ID using the Category Slug in WordPress, this tricks is original by Bavotasan and I rewrite it.

WordPress built in with nicely functions get_term_by(), and we will use it to get Category ID based on Category Slug. Here we go what we can do with get_term_by()

<?php get_term_by( $field, $value, $taxonomy, $output, $filter ) ?>

$field: (string) (required) Either ’slug’, ‘name’, or ‘id’. Default: None
$value: (string|integer) (required) Search for this term value. Default: None
$taxonomy: (string) (required) Taxonomy Name. Default: None
$output: (string) (optional) Constant OBJECT, ARRAY_A, or ARRAY_N. Default: OBJECT
$filter: (string) (optional) default is raw or no WordPress defined filter will applied. Default: ‘raw’

Sponsored Links

Let’s get practice by adding this code to get Category ID based on Category Slug

<?php
$theCatId = get_term_by( 'slug', 'tutorials', 'category' );
$theCatId = $theCatId->term_id;
?>

What’s for this Category ID? It depend on you, you can use it to add extra query or display category description. Next code is how to display category description using Category Slug

<?php
$theCatId = get_term_by( 'slug', 'tutorials', 'category' );
$theCatId = $theCatId->description;
?>

Of course you can make this code more useful, use your creativity and explore wordpress tricks and Create better code to get Category ID based on Category Slug. Learn WordPress everyday and you’ll more enjoy with it

Related Posts

  1. Custom Query String (Reloaded)
  2. How to Exclude Some Categories from Feed, Archives, Search, and Home page

Detect Category ID using the Category Slug in WordPress On The NET

How to Detect Category ID using the Category Slug in WordPress ...

WordPress built in with nicely functions get_term_by(), and we will use it to get Category ID based on Category Slug. Here we go what we can do with get_term_by() read more

Source: http://wptricks.net/how-to-detect-category-id-using-the-category-slug-in-wordpress/

How To Filter caption-Shortcode in WordPress # WordPress Tricks & Tips

How To Create a PDF viewer shortcode in WordPress; Detect Category ID using the Category Slug in WordPress; Added Permalinks Shortcode on WordPress read more

Source: http://wptricks.net/how-to-filter-caption-shortcode-in-wordpress/

Custom Query String (Reloaded) # WordPress Tricks & Tips

Detect Category ID using the Category Slug in WordPress; How to Exclude Some Categories from Feed, Archives, Search, and Home page; The Best Way to Insert Custom Content into Feeds read more

Source: http://wptricks.net/custom-query-string-reloaded/

How To Enable Shortlink in WordPress 3.0 # WordPress Tricks & Tips

How To Enable WP.ME Shortlink on WordPress 2.9; Tutorial Enable HTML markup in user profiles; Detect Category ID using the Category Slug in WordPress read more

Source: http://wptricks.net/enable-shortlink-in-wordpress-3-0/

WordPress › Support » Get active category of post in multiple ...

... sub-category ID ... m using a solution that checks the permalink (via $_SERVER['REQUEST_URI']) to get the child/sub-category slug. ... wordpress/products-page?category=2&product_id=11 read more

Source: http://wordpress.org/support/topic/352317


Amazing Touch


7 Comments

  1. I will show you how to find Category ID

  2. Thanks for sharing this, I just used it to sort a menu system out

  3. Its probably been a best way to find the category id in wordpress. I must try to apply it on my blog. I hope i ‘ll definitely get the desired results. Thanks for the tutorial mate.

  4. This seems to be a helpful piece of trick on wordpress. Let me try if this will work on my weblog. Thanks for sharing this.

  5. Think Well

    Great tip! I was wondering if there is anyway to obtain the Post ID from the Post Title or Post Name?

  6. Sorry, Out of topic.

    Can wordpress show different post from category depending user click? I mean here, from one single page, it will detect automatically category id and show post from that category.

    thank you

  7. Impressive. I haven’t tried this yet. I’ll check out later and see if it’ll work with my blog. Thanks for the information. I appreciate your help :)



Add Your Comment

Gravatar