I have a custom field set up that returns the category ID of a chosen category, I’m looking to use this category ID to show, in one instance: category slug, and another instance: category title.
The field get’s called quite simply like so: <?php the_field('category_test'); ?>
and returns the ID, in this case 4
.
Is it at all possible to use this field to return the category slug, and also use it to return the category title? I’m not really sure how or if this can be done. Any suggestions would be greatly appreciated!
get_category
will return all the information you need for both cases.From the Codex, that will give you (sample data obviously):
I am pretty sure that
get_field
is correct.get_category
works correctly for every category ID that I’ve tried, andNULL
for bad category IDs but per the discussion below,get_field
can return an array. When this happensget_category
appears to return the first category in the database by ID which is theuncategorized
category by default. That can be demonstrated with the following.Therefore, to compensate you will need:
To get any particular field just use standard object syntax. Fpr example:
Or