0

A lot of websites use it but suprisingly nobody tell us how to do it (right way).

I have few ideas but Im not sure if this was the most efficient way - Im not very good with loops (under a year of experience in programming).

Raw example:

Have lots of if statements in foreach or while loop.

// All this inside loop - I've already got while loop for my multiple markers
global $post;
$veggie_type = get_post_meta($post->ID, 'veggie_type', true);

if($veggie_type == 'carrot') {

     icon = marker_url_1;

}
else if($veggie_type == 'beet') {

     icon = marker_url_2;

}
//etc like 10 if statements

Could anyone give me an advice or example?

4
  • Not sure what you're asking. Do you just want to make your 10 if statements more compact?
    – MSTannu
    Sep 25, 2015 at 22:59
  • @MSTannu Im asking if there's a better way to add different markers to different values (doesn't matter if different meta, post type etc)? If there is none, I would take the advice how to make this more compact.
    – N00b
    Sep 25, 2015 at 23:06
  • How are the valid types of veggies defined?
    – Milo
    Sep 25, 2015 at 23:41
  • @Milo It doesn't show in my example but the values I have are taxonomies (slugs) that I need to assign different map markers to. ..or Im not fully understanding what you mean.
    – N00b
    Sep 25, 2015 at 23:52

1 Answer 1

0

Meta seems like a fine place for storing this data.

To make it more compact:

$markers = array(
    'carrot' => 'marker_url_1',
    'beet'   => 'marker_url_2',
);

$icon = isset( $markers[ $veggie_type ] ) ? $markers[ $veggie_type ] : $markers[0];

Or if you are sure you have markers for each type and you can rename the marker files, go with

$icon = 'marker_' . $veggie_type . '.png';// png format is better for small images

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.