Why isn’t get_option array contents displaying?

Let’s look at your code. Your first code block is treating an array like an object so your second attempt is closer to accurate:

$the_contents=get_option('slider_contents');
// var_dump($the_content);
foreach ($the_contents as $content) {
     $content1=stripslashes($content['content']);
     $content2=stripslashes($content['content2']);

Assuming you did what I suggested and placed the var_dump where I have in that code block, then what is happening is this: foreach ($the_contents as $content) { lets you loop through the array. At each iteration, $content is itself an array that looks like:

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

So when you try to access $content['content'] you are trying to access a key that doesn’t exist– you’ve already looped “past” that. You can demonstrate this for yourself by running:

$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); 
}

What you need to be doing is looping over that $contents array and taking each piece individually.

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

Leave a Comment