0

I have installed Advanced Custom Fields and created a color custom field for categories.

Now I want to achieve two things:

1- Display the title text of the post in the color selected in the category custom field. 2- Display category name with the background selected in the category custom field.

I'm using GeneratePress theme and hooking the inline css using the Generatepress hooks. Here's the code:

$term = get_queried_object();
$color = get_field('color', $term);
?>

<style>
    .wp-block-post-title {
        color: <?php echo $color; ?> !important;
    }
</style>

The problem is that $color variable is not populating the color value from the custom field. When I hardcode the color e.g., #000, the code works perfectly fine.

Please tell me what I'm doing wrong in the code? Thank you.

2
  • 1
    Have you tried get_queried_object_id()? get_queried_object() returns a full WP_Post object and according to ACF documentation get_field() is looking for just an ID.
    – WebElaine
    Commented Dec 8, 2021 at 16:33
  • Tried using get_queried_object_id() but it didn't work.
    – Usman
    Commented Dec 8, 2021 at 20:35

1 Answer 1

0

In the documentation, it looks like you can provide the term object to get_field(), however I've tended to use either taxonomy_#termid or term_#termid for example

$color = get_term('color', 'category_1234);

or

$color = get_term('color', 'term_1234);

If those down't work, then I'd check:

  • That your ACF field name is definitely color (and not colour, as I would write - being in the UK) and also worth doing a print_r($term); or var_dump($term);
  • To make sure you have a WP_Term object
  • Check $colour using print_r or var_dump again, and see what you're getting back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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