1

This is my current array that displays list items in a unordered list.

function get_bottle_colors() {
    if(empty($_GET['cap_id'])) return false;
    $constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
    if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
        $output = '<label>Bottle Color</label><ul>';
            foreach ($constructor_img as $key => $image) {
                if(empty($image['image'])) continue;
                $output .= '<li><a href="'.$image['image'].'" data-width="'.$img_size[0].'" data-height="'.$img_size[1].'"';
                $output .= '</a></li>';
            }
        $output .= '</ul>';
    }else{
        $output = '<label>Bottle Color</label><ul></ul>';
    }
    echo $output;
    die();
}

In total there will be up to 16 list items generated by this. I need each list item to have its own class eg: list class="red", list class="green", etc. Any idea how i go about achieving this?

4
  • Set an array prior to the loop and use an increasing counter to access the values for every new row OR set a new meta value for each product which contains that class name. Which option would you like me to explain? Commented May 1, 2015 at 18:58
  • you want class on li or on anchor and also put all classes in an array and when you are iterating your image loop based on counter fecth the value from class array and insert them in your li or anchor tag Commented May 1, 2015 at 19:00
  • Guo Cong please mark and up-vote the answer for others help. thanks Commented Feb 3, 2016 at 9:50
  • peoples are not interested in mark and up-vote the answer for others help. Also not interested in telling that problem solved or not? wastage of effort. Deleting my answer. Commented Mar 22, 2016 at 5:34

1 Answer 1

1

Found the solution thanks to Anant. Had to declare the class array like below.

function get_bottle_colors() {
    if(empty($_GET['cap_id'])) return false;
    $constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
    if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
        $output = '<label>Bottle Color</label><ul>';
        $i = 0;
        $class_array = array("a","b","c","d","e","f","g","h","i","j","k","l","n","m","n","o","p");
            foreach ($constructor_img as $key => $image) {
                if(empty($image['image'])) continue;
                $category = 9;
                $img_size = getimagesize($image['image']);
                $output .= '<li class= "'.$class_array[$i].'"><a href="'.$image['image'].'" data-width="'.$img_size[0].'" 

                data-height="'.$img_size[1].'"';
                $output .= 'data-id="'.$_GET['cap_id'].'_'.$key.'" data-part="#constructor-bottles" class="sub-caps">'.$image['title'];
                $output .= '</a></li>';     
                $i++;
            }
            $output .= '</ul>';
        }else{
            $output = '

<label>Bottle Color</label><ul></ul>';  }   echo $output;   die(); }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.