$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display a colour of custom taxonomy on the page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display a colour of custom taxonomy on the page

matteradmin9PV0评论

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>';
    }
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far