treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Printing an Array as an Unordered List

  • I'm using Bill Erickson Custom Meta Boxes and Fields for WordPress and was wondering how I'd go about printing an array of checkboxes as an unordered list.

    This is what I've got in my theme's functions.php file:

    array(
    'name' => 'Other Features',
    'desc' => 'field description (optional)',
    'id' => $prefix . 'other_features',
    'type' => 'multicheck',
    'options' => array(
    'language' => 'English Speaking',
    'furniture' => 'Modern & Classic Furniture',
    'fridge' => 'Fridge/Freezer',
    'stove' => 'Wood Burning Stove',
    'oven' => 'Electric Oven',
    'internet' => 'Free Wi-Fi',
    'tv' => 'Television/DVD Player',
    'plugs' => 'UK Power Plugs',
    )
    ),
    And this is how it appears in my Add/Edit page panel:

    image
    What I'd like to know how to do is print only the selected checkboxes as an unordered list with their ID, like so:

    <ul>
    <li id="language">English Speaking</li>
    <li id="furniture">Modern &amp; Classic Furniture</li>
    <li id="stove">Wood Burning Stove</li>
    <li id="oven">Electric Oven</li>
    <li id="internet">Free Wi-Fi</li>
    <li id="tv">Television/DVD Player</li>
    </ul>
    Any help at all to point me in the right direction would be much appreciated. Thanks in advance.
  • Here's what I ended up using, courtesy of Bill Erickson himself:

    <?php
    global $post;
    $other_features = get_post_meta( $post->ID, 'other_features' );
    echo '<ul>';
    foreach( $other_features as $id => $value )
    echo '<li id="' . $value . '">' . $value . '</li>';
    echo '</ul>';
    ?>