Skip to content

Instantly share code, notes, and snippets.

@danott
Created April 8, 2011 15:33
Show Gist options
  • Select an option

  • Save danott/910115 to your computer and use it in GitHub Desktop.

Select an option

Save danott/910115 to your computer and use it in GitHub Desktop.
WordPress function to accurately retrieve the current category ID outside the loop.
<?php
/* current_category_ID()
*
* Get the currently-being-viewed category's ID.
*
* THE PROBLEM:
* WordPress offers no easy way to get the id of the category that is currently
* being viewd in an index.php. It offers nice conventions to get the title and
* description (within the loop), but I needed this information outside the loop.
*
* THE SOLUTION:
* After searching WordPress' documentation, get_queried_object() finally emerged
* as a way to functionally get this information.
*/
function current_category_ID()
{
$category = get_queried_object();
return $category->term_id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment