Get WordPress Current Category ID and Top Level Parent Category ID on all category and post templates
As a WordPress developer, it's often necessary to access the current category ID and the top-level parent category ID on your category and post templates. This information can be valuable for a variety of purposes, such as styling the active category in your navigation, displaying breadcrumbs, or targeting specific content based on the category.
In this blog post, we'll explore a simple and effective way to retrieve the current category ID and the top-level parent category ID using built-in WordPress functions. We'll also discuss how to use this information to display a structured list of categories and style the active category.
Determining the Current Category ID
The first step is to determine the current category ID, whether we're on a category page or a post page. We can do this using the get_queried_object()
function, which returns the object that represents the current page or post.
<?php
//Get whatever object we're working with (category or post?)
$thisObj = get_queried_object();
//If it's a post, get the category ID
if(!is_null($thisObj->ID)){
$currentCat = get_the_category();
$currentCatID = $currentCat[0]->cat_ID;
}
//If it's a category, get the ID a different way
elseif(!is_null($thisObj->term_id)){
$currentCatID = $thisObj->term_id;
}
In this code, we first get the queried object using get_queried_object()
. If the object has an ID
property (meaning it's a post), we retrieve the current category ID by getting the first category associated with the post using get_the_category()
. If the object has a term_id
property (meaning it's a category), we simply use the term_id
as the current category ID.
Displaying a Structured List of Categories
Now that we have the current category ID, we can use the wp_list_categories()
function to display a structured list of all the categories. This function allows us to customize the list with various arguments, such as sorting order, whether to include empty categories, and more.
<?php
$args = array(
'current_category' => $currentCatID, //This assigns the "current-cat" class to the correct item in the list so you can style it differently
'child_of' => 0,
'hide_empty' => 0,
'order' => 'ASC',
'orderby' => 'name',
);
wp_list_categories($args);
In this example, we set the current_category
argument to the $currentCatID
we obtained earlier. This ensures that the correct category in the list is assigned the current-cat
class, which you can then use to style the active category differently.
You can further customize the list by adjusting the other arguments, such as child_of
to display only the top-level categories, hide_empty
to exclude empty categories, and order
and orderby
to control the sorting.
Displaying the Top-Level Parent Category
In addition to the current category ID, it's often useful to know the top-level parent category ID. This information can be used to display breadcrumbs or to target specific content based on the top-level category.
Here's how you can retrieve the top-level parent category ID:
<?php
$thisObj = get_queried_object(); // Find out what we're displaying (Category or post?)
if(!is_null($thisObj->ID)){ // If it's a post, get the category ID
$currentCat = get_the_category();
$currentCatID = $currentCat[0]->cat_ID;
}
elseif(!is_null($thisObj->term_id)){ // If it's a category, get the ID a different way
$currentCatID = $thisObj->term_id;
}
// Get the name of the Category we're starting with
$currentCatName = get_cat_name($currentCatID);
//Get the ID then the name of the highest parent Category
$topParentID = end(get_ancestors($currentCatID, 'category'));
$topParentName = get_cat_name($topParentID);
if(!$topParentName){ // If we're already at the highest category, just save the name
$finalAnswer = $currentCatName;
}
else{ // Otherwise, display "Top Parent > Current Cat"
$finalAnswer = $topParentName . ' > ' . $currentCatName;
}
echo $finalAnswer; //Ta-da!
In this code, we first determine the current category ID, just like before. Then, we use the get_ancestors()
function to get the IDs of all the parent categories, with the top-level parent category ID being the last item in the array.
We then use the get_cat_name()
function to retrieve the names of the current category and the top-level parent category. If we're already at the top-level category, we simply display the current category name. Otherwise, we display the top-level parent category name followed by the current category name, separated by a "greater than" symbol (>
).
Styling the Active Category
Now that we have the current category ID, we can use it to style the active category in our category list. This can be done by targeting the current-cat
class that was automatically added to the active category by the wp_list_categories()
function.
.current-cat{
font-weight: bold;
color: #333;
}
In this example, we set the font weight to bold and the color to a dark gray for the active category. You can customize the styles to fit your design preferences.
Conclusion
In this blog post, we've covered how to retrieve the current category ID and the top-level parent category ID on WordPress category and post templates. We've also discussed how to use this information to display a structured list of categories and style the active category.
By mastering these techniques, you can create more dynamic and user-friendly WordPress websites that adapt to the user's current context. Remember, the key to building successful WordPress sites is understanding and leveraging the powerful tools and functions provided by the WordPress ecosystem.
If you're looking for a comprehensive solution to analyze your website's user behavior and generate data-driven recommendations to improve your conversion rates, be sure to check out Flowpoint.ai. Flowpoint's powerful analytics and AI-powered insights can help you identify and fix technical issues, optimize your user experience, and drive more conversions
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.