Detect Category ID using the Category Slug in WordPress


Sponsored Links






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’

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

Subscribe to Full RSS Feed

RSS FeedIf you thing this article useful, please consider subscribing to our RSS Feed or e-mail updates and stay updated with us. You can also follow @WPTricksNet on twitter for latest updates.

About WP Tricks

We are WordPress Family, we collected and writed useful tips and tricks for you WordPress. We share it for you...

Related Tips and Trick from Local Data and on The Net

How to Tweak You WordPress Post Category Page

Deluxe Blog Tips give nicely example how to show different number of post each category in WordPress. This code must be put in functions.php. On Deluxe Blog Tips sample how to show number post on News and WordPress Category. On … Read More

Style Posts From Some Category Differently

Right now, every blogger has know WordPress as easiest and nicely support publishing system. To create more sophisticated and personal blog. Blogger chooses WordPress as they Blog Engine. Some featured that we can use to create more unique blog is … Read More

Custom Query String (Reloaded)

You can Download WordPress Plugin Custom Query String (Reloaded), this plugin is the perfect solutions to create and customize your WordPress Post. Custom Query String (CQS) provides a Admin Options panel whereby users may specify any number of custo

How To Detect the visitor browser on WordPress

With Extra Code, we can detect where our reader come from, that way I will show you how to detect the visitor browser on WordPress. What for? There are so many usage that we can use. Example we can delivered … Read More

How to Exclude Some Categories from Feed, Archives, Search, and Home page

Exclude Some Categories on Feed, Archives, Search and many more in WordPress  is another solutions to hidden our content from un necessary reader, there are some plugin that can handle it. Recently, Miriam found an AMAZING plugin, Simply Exclude

Detect Category ID using the Category Slug in WordPress

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 read more

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

get taxonomy id wordpress - WordPress Tricks & Tips

Detect Category ID using the Category Slug in WordPress will show you how to find Category ID using the Category Slug in WordPress read more

Source: http://wptricks.net/archives/get-taxonomy-id-wordpress?amp

How to use different header for categories and pages in WordPress

/**** Conditional Header for Per Category Example Wordpress ****/ // make sure // Alternative for using Category ID, You can use Category Name or Slug if (is_category read more

Source: http://www.ruhanirabin.com/different-headers-in-different-categories-and-pages-wordpress/

WordPress › List category posts « WordPress Plugins

ways: Using the category id, the category name or slug and to make it detect the category id of for the List Category Plugin are searched for in your WordPress read more

Source: http://wordpress.org/extend/plugins/list-category-posts/other_notes/

WordPress › Support » Get active category of post in multiple

experiencing is getting the active sub-category ID REQUEST_URI']) to get the child/sub-category slug. category and its then impossible to detect the 'active category read more

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


10 Responses to Detect Category ID using the Category Slug in WordPress

  1. wiper blades says:

    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. John says:

    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 says:

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

  6. prazetyo says:

    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. Chris says:

    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 :)

  8. Pingback: Alex Barber | Digital Artist | WordPress get category slug from its name

  9. eas system says:

    Let me try if this will work on my weblog.

  10. I hope i ‘ll definitely get the desired results. Thanks for the tutorial mate.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>