为什么没有显示GET_OPTION数组内容?

时间:2013-02-24 作者:Jim S.

我在无数的帖子中寻找答案,并尝试了几乎所有的组合。很明显,我错过了一个很小的步骤,这对Wordpress社区来说可能是深不可测的。

我的代码将选项写入数据库,但无法将其显示回来。未设置数组。可以添加和删除多个选项。

写入数据:

<?php
     $mycontents = array(\'content\' => $_POST[\'cont\'], \'content2\' => $_POST[\'cont2\']);
     update_option(\'slider_contents\',$mycontents);
?>
以下是数据库条目:

a:2:{s:7:"content";a:3:{i:0;s:19:"This is content 1-a";i:1;s:19:"This is content 2-a";i:2;s:19:"This is content 3-a";}s:8:"content2";a:3:{i:0;s:19:"This is content 1-b";i:1;s:19:"This is content 2-b";i:2;s:19:"This is content 3-b";}}
正在尝试读取和显示数据:

<?php
    $the_contents=get_option(\'slider_contents\');
    foreach ($the_contents as $content) {
         $content1=stripslashes($content->content);
         $content2=stripslashes($content->content2);
?>
<li><textarea name="cont[]" rows="3" style="width:70%;" ><?php echo $content1; ?></textarea><br><textarea name="cont2[]" rows="3" style="width:70%;" ><?php echo $content2; ?></textarea><br><input type="button" value="Delete this option" onClick="delete_field(this);"  /><input type="button" value="Add new option" onClick="add_to_field(this);"  /></li>

        <?php } ?>
我也试过。。。

<?php
    $the_contents=get_option(\'slider_contents\');
    foreach ($the_contents as $content) {
         $content1=stripslashes($content[\'content\']);
         $content2=stripslashes($content[\'content2\']);
?>
的输出var_dump($the_contents); 是:

array(2) { 
  ["content"]=> array(3) { 
    [0]=> string(19) "This is content 1-a" 
    [1]=> string(19) "This is content 2-a" 
    [2]=> string(19) "This is content 3-a" 
  } 
  ["content2"]=> array(3) { 
    [0]=> string(19) "This is content 1-b" 
    [1]=> string(19) "This is content 2-b" 
    [2]=> string(19) "This is content 3-b" 
  } 
}

1 个回复
SO网友:s_ha_dum

让我们看看您的代码。第一个代码块将数组视为对象,因此第二次尝试更接近准确:

$the_contents=get_option(\'slider_contents\');
// var_dump($the_content);
foreach ($the_contents as $content) {
     $content1=stripslashes($content[\'content\']);
     $content2=stripslashes($content[\'content2\']);
假设你按照我的建议做了var_dump 在代码块中,发生的是:foreach ($the_contents as $content) { 用于在阵列中循环。在每次迭代中,$content 本身是一个数组,如下所示:

array(3) { 
    [0]=> string(19) "This is content 1-a" 
    [1]=> string(19) "This is content 2-a" 
    [2]=> string(19) "This is content 3-a" 
}
所以当你尝试访问$content[\'content\'] 您正在尝试访问一个不存在的密钥——您已经“绕过”了该密钥。您可以通过跑步来证明这一点:

$the_contents = unserialize(\'a:2:{s:7:"content";a:3:{i:0;s:19:"This is content 1-a";i:1;s:19:"This is content 2-a";i:2;s:19:"This is content 3-a";}s:8:"content2";a:3:{i:0;s:19:"This is content 1-b";i:1;s:19:"This is content 2-b";i:2;s:19:"This is content 3-b";}}\');
foreach ($the_contents as $content) {
  var_dump($content); 
}
你需要做的是在上面循环$contents 排列并分别取下每一块。

foreach ($the_contents as $content) { // this part you already have
  foreach ($content as $c) {
    echo stripslashes($c);
    // you are building a string, of course, but that is the idea
  }
}

结束

相关推荐

WordPress theme options error

我正在构建一个主题,我需要调用主题选项,但当我在这里调用主题选项时,我确实会遇到诸如白色屏幕和500内部服务器错误之类的错误在WP查询中(PHP新手) <?query_posts (\'meta_key=post_views_count&orderby=meta_value_num&order=DESC&meta_value=get_option(mytheme_value);\');?> 也在我的前端表单中// ADD THE FORM INPUT TO $