• Resolved Drding

    (@drding)


    Hi there,

    I’m trying to use the Magic Fields 2 plugin to make menus for a restaurant website using WP.

    I was able to query a post that contained custom fields on the index page like this:

    <?php
    $query1 = get_post(219);
    $title = $query1->post_title;
    echo '<div id="specialstitle">'.$title.'</div>';
    ?>
    	<div class="stripebackbrown">&nbsp;</div>
    	<div id="specialslist">
    
    <?php $itemname = get_post_meta ($post->ID, 'menu_item_name', false); ?>
    <?php $description = get_post_meta ($post->ID, 'menu_item_description', false); ?>
    
    <?php foreach (array_combine($itemname, $description) as $itemname => $description){
    
    echo '<span>' .$itemname. '</span>';
    
    echo '<p>' .$description. '</p>';
    
    } ?>

    What this does by the way is display each item name and its description in a list.

    Here’s my problem. The above code works on the index.php page but when I try to use it on a page with a custom template I get these two errors:

    Warning: array_combine() [function.array-combine]: Both parameters should have at least 1 element in … on line 20

    Warning: Invalid argument supplied for foreach() in … on line 20

    Any ideas why this works on the index and not on a page? If I can get this working I can make several posts for each menu section (like Lunch or Desert for example) and then just query them and display the output.

    I’ve tried creating the same exact group and fields for the page post type by the way (I thought maybe that was it) and I’m still getting the above errors.

    Thanks!

    https://www.ads-software.com/plugins/magic-fields-2/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author hunk

    (@hunk)

    ok, $itemname or $description is empty.

    when uses get_post_meta the first parameter is the postID, in this case is

    <?php $itemname = get_post_meta (219, ‘menu_item_name’, false); ?>
    <?php $description = get_post_meta (219, ‘menu_item_description’, false); ?>

    or

    <?php $itemname = get_post_meta ($query1->ID, ‘menu_item_name’, false); ?>
    <?php $description = get_post_meta ($query1->ID, ‘menu_item_description’, false); ?>

    Thread Starter Drding

    (@drding)

    Hunk thank you so much, those work perfectly on the custom template pages!

    This has been driving me nuts for weeks. Awesome plugin by the way, thanks again!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Referencing a post with custom fields on a page’ is closed to new replies.