0

I've created a custom taxonomy called "colour"

// Taxonomy

add_action( 'init', 'create_colour_taxonomy' );

function create_colour  _taxonomy() {
    $labels = array(
        'name'                           => 'Colours',
        'singular_name'                  => 'Colour',
        'search_items'                   => 'Search Colours',
        'all_items'                      => 'All Colours',
        'edit_item'                      => 'Edit Colour',
        'update_item'                    => 'Update Colour',
        'add_new_item'                   => 'Add New Colour',
        'new_item_name'                  => 'New Colour Name',
        'menu_name'                      => 'Colour',
        'view_item'                      => 'View Colour',
        'popular_items'                  => 'Popular Colour',
        'separate_items_with_commas'     => 'Separate colours with commas',
        'add_or_remove_items'            => 'Add or remove colours',
        'choose_from_most_used'          => 'Choose from the most used colours',
        'not_found'                      => 'No colours found'
    );

    register_taxonomy(
        'colour',
        'page',
        array(
            'label' => __( 'Colour' ),
            'hierarchical' => false,
            'labels' => $labels
        )
    );
}

I've also created a color (colorpicker) field in the 'colour' taxonomy called 'colour_acf' with ACF PRO (screenshot attached).

All I want is to display the colours ('colour_acf') of the custom taxonomy 'colour' on the page. For example:

I have a page "Product #1" where I add with my custom taxonomy 'colour' the colours: red, blue, yellow. I've add the required colors for this taxonomies with ACF colorpicker. How can I display that colorpicker values?

Thanks! enter image description here enter image description here

  • Sure, but that example is not relevant in my case. I can add the echo values on my taxonomy page template but in my case I need to display the values of colour_acf on the page with a required taxonomy – Jamdev Dec 1 '18 at 17:02
  • It's relevant, you just need to pass the IDs for each term you've assigned to your post. The get_field examples show you how to load a field from a term. – Milo Dec 1 '18 at 17:08
  • @Milo could you please let me know how can I do that? Much appreciate – Jamdev Dec 1 '18 at 17:11
1

The colours are associated to the individual terms, so the process is to get the terms for the current post, then load and display the field for each term.

$terms = get_the_terms( get_the_ID(), 'colour' );
if( $terms && ! is_wp_error( $terms ) ){
    foreach( $terms as $term ){
        // show color code
        echo get_field( 'colour_acf', 'colour_' . $term->term_id );
        // or insert color code into background-color of a div
        echo '<div style="background-color:' . get_field( 'colour_acf', 'colour_' . $term->term_id ) . '">&nbsp;</div>';
    }
}
  • Thank you so much! But please let me know how can I DISPLAY the colour on my page.php for each page? – Jamdev Dec 1 '18 at 18:35
  • see above, added a line that inserts color into a div. this code would go into your page.php template wherever you want these colors displayed. – Milo Dec 1 '18 at 18:43

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.