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!
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 ) . '"> </div>';
}
}
get_field
examples show you how to load a field from a term. – Milo Commented Dec 1, 2018 at 17:08