admin管理员组文章数量:1130349
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 |1 Answer
Reset to default 1The 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>';
}
}
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_fieldexamples 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
1 Answer
Reset to default 1The 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>';
}
}
本文标签: Display a colour of custom taxonomy on the page
版权声明:本文标题:Display a colour of custom taxonomy on the page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749135913a2321363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


get_fieldexamples show you how to load a field from a term. – Milo Commented Dec 1, 2018 at 17:08