WordPress:从帖子中循环多个自定义字段 [英] WordPress: Looping multiple custom fields from a post

查看:149
本文介绍了WordPress:从帖子中循环多个自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近来了一个使用自定义字段会非常方便的项目.它用于餐厅,如果我做对了,我可以在网站的几个不同部分中使用这段代码.

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.

我正在使用Magic Fields 2插件,它现在很棒,除了要显示我创建的字段时.这就是我迷路的地方.

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.

例如,我有一个名为"Specials"的帖子,该帖子具有一个名为"Menu Item"的分组,该分组具有两个名为"Name"和"Description"的字段.名称基本上是用作菜单项的名称,描述将是关于食品项的一小部分.

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 while the above code is technically working, it's not working properly at the same time. Any help would be greatly appreciated!

我在此方面取得了一些更好的成功:

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--> 

但是,我不确定如何在$ foreach循环中添加$ description.因此,现在它列出了菜单名称,但在应该添加描述的地方添加了"Array".越来越近!

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!

推荐答案

我知道这很旧,但是我遇到了同样的问题,因此我尝试对@speccode答案进行一点点改进,但​​似乎可行:

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>';
  } 
?>

这篇关于WordPress:从帖子中循环多个自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆