0

I'm having some difficulty functioning this morning! This isn't specifically WP (more PHP), but I think that it would benefit WP devs in the future, so I'm asking here. I'm trying to create a list that does something like this:

$items = get_post_meta($post->ID, "item-specific-meta");
$output = '';
foreach ($items as $item){
  $item_id = $item[#] <--I WANT THIS TO BE A NUMBER
  $output .= '<label for="item-';
  $output .= $item_id;
  $output .= '"><input id="item-';
  $output .= $item_id;
  $output .= '"/>';
  $output .= $item;
  $output .= '</label>';
}

Which would return something like:

<label for="item-1">
  <input id="item-1"/>
  Item 1
</label>

Right now I'm putting the content of the custom meta item in place of the numbered list that I would prefer using. It does the job, but I just don't like all that excess crap that could be done simply.

Like I opened with, my brain isn't working right this morning and I can't even think of the proper search term to Google and answer this myself, so I'm turning to the awesome folks here! Thanks in advance!

4
  • @s_ha_dum is there a way to move this to Stack Overflow? Thanks for catching this for me! Commented Feb 7, 2014 at 16:27
  • 1
    It looks like you are asking how to increment a variable. That is pretty basic. If this were migrated, I would expect it to be poorly received.
    – s_ha_dum
    Commented Feb 7, 2014 at 16:36
  • Is there any way to delete the question, while allowing tf to maintain the rep. earned? Either way I take no offense to the removal of the question. It is a very basic question, I just wasn't able to think of what I was trying to do this morning for some strange reason. Commented Feb 7, 2014 at 20:58
  • 1
    No. There is no way to delete this question and preserve the rep earned from answers. I asked in the mod forum, just to be sure, and this was confirmed.
    – s_ha_dum
    Commented Feb 7, 2014 at 22:49

1 Answer 1

2

This is pure PHP.

$items = get_post_meta($post->ID, "item-specific-meta");
$item_id = 1;
$output = '';
foreach ($items as $item) {
    $output .= '<label for="item-'.$item_id.'"><input id="item-'.$item_id.'"/>'.$item.'</label>';
    ++$item_id;
}
1
  • Thank you so much dude! I knew it was something simple! You seriously just made my day! Commented Feb 7, 2014 at 16:24

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