I use Wordpress Custom Fields to provide customers with various product color codes in a drop-down list, but now I would like to add the various color thumbnails for ease of selection...similar to how color is selected in this site: http://www.blackhairspray.com/outre-quick-weave-belinda-outreqsbel.html

Can anyone give me an idea of how to do this in wordpress please or point me to a relevant tut?

since googling has failed to provide the goods so i am eagerly holding my breathe for a SuperUser delivery :-)

migrated from superuser.com Jun 16 '11 at 14:53

This question came from our site for computer enthusiasts and power users.


Store each custom field as a comma separated pair. Like this:

option_name, option_image_URL

Then we do this (within the loop)...

$options = array();
foreach( get_post_custom_keys( $post->ID ) as $key )
    $a = get_post_meta( $post->ID, $key, true )
    $a_array = explode( ',', $a );
    $options[] = $a_array[0] // first value of each array is the option, add it to another array
    echo '<img src="' . $a_array[1] . '" id="' . $a_array[0] . '"/>'; // display the image with the option name as the id attribute


Then loop through the options array:

<select name="your_option">
foreach( $options as $option )
   echo '<option id="' . $option . '" value="' . $option . '">' . $option . '</option>';

Each of the images should have an id attributes should have an ID attribute that's the same as one of the drop down options. You can then use some javascript to change the options around.

This javascript probably won't work, but it will get you started:

    var id = jQuery(this).attr('id');
    jQuery( 'option' ).removeAttr( 'selected' );
    jQuery( 'option #' + id ).attr( 'selected', 'selected');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy