WordPress: Looping multiple custom fields from a post

ε祈祈猫儿з 提交于 2019-12-12 05:49:08

问题


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!

EDIT:

I'm having some better success with this:

<div id="specials">

<?php 
$query2 = get_post(28);
$title = $query2->post_title;
?>
    <div id="specialstitle"><?php echo $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 ($itemname as $itemname){

echo '<span>' .$itemname. '</span>';

echo '<p>' .$description. '</p>';

} ?>
    </div>


</div><!-- end specials--> 

However, I'm not sure how to add $description to that foreach loop. So right now it's listing out the menu names but adding "Array" where the description should be. Getting closer!


回答1:


I think you want to do this like that:

<div id="specials">
<?php 
$query2 = get_post(28);
$title = $query2->post_title;

?>
    <div id="specialstitle"><?php echo $title; ?></div>
    <div class="stripebackbrown">&nbsp;</div>
    <div id="specialslist">
<?php while ( have_posts() ) : the_post();

$itemname = get_post_meta(get_the_ID(), 'menu_item_name', true);
$description = get_post_meta(get_the_ID(), 'menu_item_description', true);

echo '<span>'.$itemname.'</span>';
echo '<p>'.$description.'</p>';

endwhile; 
wp_reset_query();
?>
    </div>




回答2:


Found a solution:

<?php 
$query2 = get_post(28);
$title = $query2->post_title;
?>
    <div id="specialstitle"><?php echo $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>';

} ?>
    </div>


</div><!-- end specials-->

This combines the two arrays created by $itemname and $description and loops them as many times as there are groups to loop.

@speccode, thanks so much for trying to help me, your reordering of my two lines got me heading in the right direction.




回答3:


I know this is old but I had the same problem so I attempted to improved @speccode answers' a tiny bit and it seems to work:

<?php $itemnames = get_post_meta($post->ID, 'itemnames', false); ?>
<?php $descriptions = get_post_meta($post->ID, 'descriptions', false); ?>

<?php foreach (array_combine($itemnames, $descriptions) as $itemname => $description)
   {
     echo '<img src="'.$itemname.'"/>';
     echo '<p>' .$description. '</p>';
  } 
?>

I also attempted abit of structuring in there (see below) but I can't guarantee the validity of the markup... anyone?

<?php $itemnames = get_post_meta($post->ID, 'itemnames', false); ?>
<?php $descriptions = get_post_meta($post->ID, 'descriptions', false); ?>

<?php foreach (array_combine($itemnames, $descriptions) as $itemname => $description)
   {
     echo '<div class="nameAndDescription"><img src="'.$itemname.'"/>';
     echo '<p>' .$description. '</p></div>';
  } 
?>


来源:https://stackoverflow.com/questions/19918016/wordpress-looping-multiple-custom-fields-from-a-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!