• I’ve recently come to a project where using custom fields would be pretty handy. It’s for a restaurant and if I can get this right, I could use this bit of code for a few different sections of the site.

    I’m using the Magic Fields 2 plugin and right now it’s awesome, except for when it comes to displaying the fields I’ve created. Here’s where I’m getting pretty lost.

    For example, I have a post called “Specials” and that has a grouping called “Menu Item” and that group has two fields called “Name” and “Description”. Basically name serves as the menu item name, and description will be a small blurb about the food item.

    Here’s the code in my theme for this section right now:

    <div id="specials">
    <?php
    $query2 = get_post(28);
    $title = $query2->post_title;
    $itemname= get_post_meta($post->ID, 'menu_item_name', true);
    $description = get_post_meta($post->ID, 'menu_item_description', true);
    
    ?>
        <div id="specialstitle"><?php echo $title; ?></div>
        <div class="stripebackbrown">&nbsp;</div>
        <div id="specialslist">
    <?php while ( have_posts() ) : the_post();
    
    echo '<span>'.$itemname.'</span>';
    echo '<p>'.$description.'</p>';
    
    endwhile;
    wp_reset_query();
    ?>
        </div>
    
    </div><!-- end specials-->

    Now there is a very good chance what I’m doing in the above code is completely wrong, if it is, please let me know. What I’m experiencing with the above code however is it’s pulling the very first group for that post.

    So instead of something like:

    Tomato Soup
    description text here

    Turkey Sandwich
    description text here

    Greek Salad
    description text here

    I’m getting:

    Tomato Soup
    description text here

    Tomato Soup
    description text here

    Tomato Soup
    description text here

    So while the above code is technically working, it’s not working properly at the same time. Any help would be greatly appreciated!

  • The topic ‘Looping custom fields from defined post’ is closed to new replies.