在同一自定义字段键中存储多个值并一次打印
如果要将其存储为站点选项,可以使用update_option()
:
http://codex.wordpress.org/Function_Reference/update_option
Example 1:
// some array to store:
$items=array(\'yellow\',\'orange\',\'green\');
// save the array
update_option(\'myitems\',$items);
// get the array
$items=get_option(\'myitems\');
// print the array
echo "<ul>";
foreach($items as $item){
echo "<li>".$item."</li>";
}
echo "</ul>";
如果要将其存储为post meta(即,对于每个帖子),可以使用
update_post_meta()
http://codex.wordpress.org/Function_Reference/update_post_meta
Example 2:
// some array to store:
$items=array(\'yellow\',\'orange\',\'green\');
// save the array
update_post_meta($post_id,\'myitems\',$items);
// get the array
$items = get_post_meta($post_id,\'myitems\',true);
// print the array
echo "<ul>";
foreach($items as $item){
echo "<li>".$item."</li>";
}
echo "</ul>";
Example 3:
如果要从后端添加自定义字段(相同的元键)和值,如下所示:
您可以检索如下值:
// get the array for current post
$items = get_post_meta($post->ID,\'myitems\'); // we skip the true part here
// print the array
echo "<ul>";
foreach($items as $item){
echo "<li>".$item."</li>";
}
echo "</ul>";