PHP variable is not working in a Wordpress header and index file?
You're dealing with a variable scoping issue.
Since the inclusion of the header.php is done by the get_header()
function, the $body
variable within the header.php file is contained within the scope of that function (i.e. it is a local variable in the function).
You need to declare the variable as global within the header.php file. Before doing anything else with the variable, add this line to header.php:
global $body;
I believe this should help achieve the behavior you're looking for.
See these for reference:
Actually what is happening is much simpler than that. The header.php file is being called before index.php. If you were to delete all the HTML and just look at the PHP, you'd see this.
<body id="<?php echo $body; ?>"> <?php $body = "home"; include_once 'localization.php'; get_header(); ?>
You're just trying to echo the value of $body
before it was ever set.