Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question
    
You don't really explain what doesn't work-- "something wrong" is essentially meaningless-- but I expect that you don't have a $post variable or it isn't set to what you expect to be. Add debugging information and more detail about the problem. –  s_ha_dum Mar 29 '13 at 14:16
    
It means the value is NULL but on other pages everyting ok. How to set the $post varriable correctly? –  Rolnin Mar 29 '13 at 14:55
    
Notice: Undefined index: background_image_meta I turned on the debug, this is the result. –  Rolnin Mar 29 '13 at 15:01
    
See if this helps: wordpress.stackexchange.com/questions/91249/… –  s_ha_dum Mar 29 '13 at 15:16
    
If the page being shown does not have an associated post, where would the post meta/custom fields come from? They cant just eb created out of thin air, they need to come from somewhere. Clearly this is a failure to provide default values –  Tom J Nowell Mar 30 '13 at 1:22

2 Answers 2

up vote 1 down vote accepted

The basic problem is that there is no variable $post in your header.php. That variable might exist in the global scope, but your code operates in a function scope of load_template() which was called by get_header().

So you have four options:

  1. Import the global variable into your function with the global keyword. global $post;

    // make sure everything is set up as a post object
    $post   = get_post( $post );
    $values = get_post_custom( $post->ID );
    
  2. Use get_queried_object_id() to get the ID, similar to hepii110’s suggestion.

    $values = get_post_custom( get_queried_object_id() );
    
  3. Use get_the_ID(). This does almost the same as version 1.

    $values = get_post_custom( get_the_ID() );
    
  4. Call get_post_custom() without the post ID. It will try to find the correct ID automagically.

    $values = get_post_custom();
    
share|improve this answer

change

$values = get_post_custom( $post->ID ); 

to

$values = get_post_custom( get_queried_object()->ID );
share|improve this answer
3  
Please explain why that could solve the problem. –  toscho Mar 30 '13 at 0:32
    
I tested it. I created a static page, and used as a blog page. I tried to get the custom value with the first code $values = get_post_custom( $post->ID ); but it returns an other post id. The $values = get_post_custom( get_queried_object()->ID ); line returns the correct post id. (The id of the static blog page) –  hepii110 Mar 30 '13 at 14:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.