Display a colour of custom taxonomy on the page

admin2025-06-05  1

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!

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!

Share Improve this question asked Dec 1, 2018 at 16:54 JamdevJamdev 1175 bronze badges 4
  • Have you tried the examples in the documentation? – Milo Commented Dec 1, 2018 at 17:00
  • 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 Commented Dec 1, 2018 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 Commented Dec 1, 2018 at 17:08
  • @Milo could you please let me know how can I do that? Much appreciate – Jamdev Commented Dec 1, 2018 at 17:11
Add a comment  | 

1 Answer 1

Reset to default 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>';
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749135945a316664.html

最新回复(0)