I'm using the ACF Taxonomy field add-on to select a category and (hopefully) return details for that category, e.g. slug, title etc.
The custom field get_field('category_test') returns the category ID, in this case being 4, I then want to use this category ID to fetch the category slug in one instance, and the category title in another.

I'm trying this in my functions.php:

function get_cat_slug($cat_id) {
$cat_id = (int)$cat_id;
$category = &get_category($cat_id);
return $category->slug;

and calling it in my page.php using echo get_cat_slug(get_field('category_test')); but the slug that is being returned is uncategorized instead of the category 'test' with the ID of 4. Even trying this $catinfo = get_category(get_field('category_test')); returns the array for uncategorized, everything I try doesn't seem to work, is there any reason for this? Any suggestions would be greatly appreciated!


Clearly this is because the ID you are passing to get_category is wrong (false or 0). i don't know how you configured the taxonomy add on but do a vaR_dump and double check what data the get_field function is returning, maybe it's an array with a single value in it :


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.