Hi folks, I have a custom post type using the default category taxonomy, same as normal posts. I’ve set up an ACF color picker for the category taxonomy, so each category can have an accent colour chosen for it.
I’m trying to add a function to allow me to return a different background colour for a post query loop, based on the ACF colour picker value pulled from the category of the posts shown. I can’t seem to get it to work… I’m using the {echo:category_color} option in the dynamic data box on the post query loop Background colour settings.
Here’s my custom function…loaded into WPCodebox, running on plugins_loaded hook at priority 10…
function category_color() {
// Get the current category
$category = get_the_category();
// Get the value of the custom field from the category
$color_picker_value = get_field('category_accent_colour' , $category);
// Return the color picker value
return $color_picker_value;
}
It isn’t working. I’ve tried a bunch of variations on pulling the custom post type category including get_queried_object to echo a term_id (or not, as it appears in my case!). It’s just using the normal post categories on the custom post type, so I think it is still just the standard category taxonomy, not a custom one.
Can anyone give me a clue as to where I’m going wrong please? I’m fairly noobish around PHP. I’m thinking maybe the get_the_category isn’t compatible with the Bricks post query, but there must be a way to do it?
I’d put this in functions.php or my own plugin. I don’t use codebox. However it’s done, just put the function ‘anywhere’ - it doesn’t need to fire - just needs to be available.
Then, where you want to set background color, open color picker - at the bottom choose RAW and put {echo:category_color()}
It’s late and I’m just writing - it’s probably not quite right, but should get you in the right direction. If you have a loop of posts and want to color based on category.
You need to call the function from inside the page - or it won’t know the page ID, for the get_the_category… and it’s no use hooking it anywhere - as it will need to fire for every query item.
See ‘advanced echo’
Hi Patrick, yeah I tried that, unfortunately. I ran this past another forum and they suggested it was because I was trying to pull a taxonomy custom field inside the post loop, so it wouldn’t see it, and I’d have to write a function to grab it.
Thanks very much for taking the time to write this! I can’t seem to get it to work, though. I’ve tried a bunch of variations on the getting the id part, and I’m using the echo part of the dynamic data… {echo:category_color} but it just never seems to output.
I’ve tried it in functions.php as well as wpcodebox. The only bit I don’t understand is “call the function from inside the page” - I’m using the bricks query loop to output a grid of posts. Shouldn’t it know the PostID from the post loop query, and be able to call the category from that? If not, how do I call this function so it runs for the post queries on this page?
Thanks so much for this. I’ll give it a try. I’m only seeing it now…for some reason I wasn’t seeing notifications of replies, and was just browsing through my old posts today.