1

I seem to be having issues getting the items in an array:

header.php

<?php
  $arr_Visited = array("V" => "2");
?>

single.php

<?php
    var_dump($arr_Visited);
?>

Which gives me this error:

Notice: Undefined variable: arr_Visited in /Applications/MAMP/htdocs/wp-content/themes/new_theme/single.php on line 4

Which is this var_dump($arr_Visited);

however when I add var_dump($arr_Visited); under the array decleration in the header.php file it works:

array(1) { ["v"]=> string(1) "2" }

Am I missing something blatantly obvious?

JamesG
  • 1,552
  • 8
  • 39
  • 86
  • Do you realize this array is in one file and accessing in another `header.php` and `single.php` – Abdulla Nilam Aug 17 '16 at 21:37
  • I thought that they will render as one whole project? I have defined the post id in the header and us it in the side bar. Is there anyway to get it to cross over? – JamesG Aug 17 '16 at 21:39
  • use `include` or `require`. Check post id must have some of the link with both – Abdulla Nilam Aug 17 '16 at 21:40
  • @Spartan the single.php has the WordPress Function get_header() which includes it. – JamesG Aug 17 '16 at 21:41
  • you can make the one in header.php a global variable and then access it in your single.php – Igor Yavych Aug 17 '16 at 21:43
  • @IgorYavych I added 'global' before the array declaration but it crashed the page – JamesG Aug 17 '16 at 21:44
  • @JamesG well, you did it incorrectly then. `global $arr_Visited; $arr_Visited = array("V" => "2");` in header.php.`global $arr_Visited; if(isset($arr_Visited)){var_dump($arr_Visited);}` in single.php – Igor Yavych Aug 17 '16 at 21:46
  • As a side-not, this is *not the WordPress way*. You should not have logic / code like this in your theme templates, they should all live in your functions.php file. – random_user_name Aug 17 '16 at 21:49

0 Answers0