<?php
/*
Plugin Name: HeadImage
Plugin URI: http://www.holycow.com/joe/archives/2005/01/25/headimage-wordpress-plugin/
Description: Add a header image style based on the meta tag from the first post on any page.  Based on Dougal Campbell's HeadMeta plugin.
Author: Joe Fulgham
Author URI: http://www.holycow.com/joe/
Version: .6

You can add a custom field named "head_image" to each entry.  This will be converted into a style modifying your #masthead when that post is the first displayed on a page.

Examples:

  key: head_image
  val: header_1.jpg

  result: <style> #masthead { background: url('/images/header_1.jpg'); } </style>

*/

function headimage() {
    global 
$posts,$post_meta_cache;
    
// Define style name
    
$headerstyle "#masthead";
    
// Define style image directory (no trailing slash)
    
$imagedir "/images";

    
// Get the first post on a page
    
$post $posts[0];

    
// Get the keys and values of the custom fields:
    
$id $post->ID;
    
$imagevals $post_meta_cache[$id]['head_image'];

    
// Generate the tags
    
if (count($imagevals)) {
        foreach (
$imagevals as $headimage) {
            
$tag "<style> $headerstyle { background: url('$imagedir/$headimage'); } </style>";
            print 
"$tag\n";
        }
    }
}

// Hook into the Plugin API
add_action('wp_head''headimage');

?>