0

I have created a colour picker field with ACF which shows on every category page and allows the end user to assign a colour to each category. This is working well but I would like the categories on my main navigation to also be assigned the colour given on the colour picker field.

I have a walker class and the bit I think I need to implement this on would be the bit of code bellow. So, I guess I need to add an inline style of "colour:" if the menu item is taxonomy with the variable that holds the category colour from the colour picker field. Something like the bellow:

        $item_output = $args->before;
        if( $item->object == 'category' ) {
            $post_categories = get_the_category();
            foreach( $post_categories as $post_category ) {
                $category_color = get_field( 'category_color', $post_category);
                $item_output .= '<a style="color:' . $category_color . ';"'. $attributes .'>';
            }
        } else {
            $item_output .= '<a'. $attributes .'>';
        }
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= ( $args->has_children ) ? '<i class="fa fa-angle-down"></i></a>' : '</a>';
        $item_output .= $args->after;

The code above is assigning the same colour to all category menu items on the main menu. I'm trying now to figure out how to assign the colour chosen on each category page colour picker field to each category menu item.

  • To track things down a bit, is the correct colour coming out at $category_color? If you inspect the CSS, is it being overriden by anything? – Tim Malone Mar 8 '16 at 2:18
  • 1
    You should post your solution as an answer and not an update to your question. You are allowed and encouraged to post solutions to your own questions and accept it. ;-) – Pieter Goosen Mar 8 '16 at 5:43
  • Ok, just posted my solution as an answer :) – Madeirense Mar 8 '16 at 5:46
3

Got it to work, finally!

Nothing like a good sleep to restart your brain :)

Updated code that works:

$item_output = $args->before;
if( $item->object == 'category' ) {
    $category = get_category( $item->object_id );
    $category_color = get_field( 'category_color', $category);
    $item_output .= '<a style="color:' . $category_color . ';"'. $attributes .'>';
} else {
    $item_output .= '<a'. $attributes .'>';
}
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= ( $args->has_children ) ? '<i class="fa fa-angle-down"></i></a>' : '</a>';
$item_output .= $args->after;

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.