get_category
will return all the information you need for both cases.
$catinfo = get_category(get_field('category_test'));
From the Codex, that will give you (sample data obviously):
stdClass Object ( [term_id] => 85 [name] => Category Name [slug] => category-name [term_group] => 0 [term_taxonomy_id] => 85 [taxonomy] => category Return category slug / title from category ID => [parent] => 70 [count] => 0 [cat_ID] => 85 [category_count] => 0 [category_description] => [cat_name] => Category Name [category_nicename] => category-name [category_parent] => 70 )
I am pretty sure that get_field
is correct.
get_category
works correctly for every category ID that I’ve tried, and NULL
for bad category IDs but per the discussion below, get_field
can return an array. When this happens get_category
appears to return the first category in the database by ID which is the uncategorized
category by default. That can be demonstrated with the following.
var_dump(get_category(array(8,9,10)));
Therefore, to compensate you will need:
$cat_field = get_field('category_test');
if (is_array($cat_field)) {
$cat_field = $cat_field[0];
}
$catinfo = get_category($cat_field);
To get any particular field just use standard object syntax. Fpr example:
echo $catinfo->slug;
Or
$cname = $catinfo->name;