First, I have a static page for homepage and for blog page too. I created a custom box for pages to setup unique background image for the pages. My idea is working on all page templates, but on blog page something wrong.
header.php is same on all page
<head>
{...}
<?php $values = get_post_custom( $post->ID ); ?>
<style type="text/css">
body { background-image:url("<?php echo $values['background_image_meta'][0]; ?>"); }
</style>
{...}
</head>
The basic problem is that there is no variable
$post
in yourheader.php
. That variable might exist in the global scope, but your code operates in a function scope ofload_template()
which was called byget_header()
.So you have four options:
Import the global variable into your function with the
global
keyword.global $post;
Use
get_queried_object_id()
to get the ID, similar to hepii110âs suggestion.Use
get_the_ID()
. This does almost the same as version 1.Call
get_post_custom()
without the post ID. It will try to find the correct ID automagically.change
to