WP plugin: Post Image
Post Image is a WordPress 2 plugin that displays an image attachment for a post through the plugin’s
szub_post_image()) template tag. It’s useful if you want to provide a unique “post image” to each post without having to manage the image references and tags within each post.
If you’re not too sure why someone might want this or what uses you could put it to, this WP support forums’ thread may help explain things further. A few points of interest:
- Displays the first image attached to the post, or alternatively an image using either the postname (i.e. post slug) or post ID of that post for its filename (i.e. title-of-post.jpg).
- Uses either the full image or thumbnail (see info on ‘use_thumb’ parameter below).
- You can specify which image attachment to use by inserting the text ‘
post-image‘ (or what is set for the ‘customkey’ parameter) somewhere in the image’s title field.
- You can define a default image (through the ‘default_image’ parameter) for posts without an image attachment.
To install Post Image, download the zip file, extract post-image.php from it, upload this file to your wp-content/plugins directory in your WordPress installation, and activate Post Image under Plugins. To use, the template tag
<?php post_image(); ?> (or
<?php szub_post_image(); ?>) must be placed somewhere in The Loop portion of your theme’s templates.
<?php post_image('default_image', use_thumb, img_tag, 'css_class', 'customkey', display); ?>
<?php szub_post_image('args'); ?>
- (string) URL to an image displayed when no image attachment exists for a post. There is no default.
- (boolean) Display the thumbnail rather than the uploaded image attachment itself. Set to
1) to use thumbnail image. Default is
- (boolean) Display in an <img> (image) tag. The plugin will automatically populate title (with attachment title), alt (same as title), and width and height attributes for image. Set to
0) to display the url *raw*. Default is
- (string) CSS class name provided to the <img> tag. Use this for styling your image(s). Default is
- (string) Image “attachment” custom field key. Use this—with the url to an image as the custom field’s value—to override the image attachment and/or default image for individual posts. Default is ‘post-image’.
- (boolean) Whether to display the image (or URL) or return it for use in other code. Set it to
0) to return only. Default is
* For boolean parameters, use only the value in parentheses (1 or 0) with szub_post_image().
<?php post_image(); ?>
Defaults: No default image for posts without an image attachment; display full image attachments (not thumbnails); display in an <img> tag; use ‘post-images’ for the css class.
<?php post_image('/blog/wp-content/images/post.jpg', true); ?>
Use ‘/blog/wp-content/images/post.jpg’ as the default image, display thumbnails for image attachments; display in an <img> tag; use ‘post-images’ for the css class.
<a href="<?php the_permalink(); ?>"><img src="<?php post_image('/images/posticon.png', false, false); ?>" width="32" height="32" /></a>
Displays image attachment URL in an <img> tag (within a link <a> element), which is the permalink to the post. Parameters are: use ‘/images/posticon.png’ as the default image; display full image attachments (not thumbnails); display the URL in raw form (no tag).
<?php $thumb = szub_post_image('use_thumb=1&display=0'); ?>
<?php if($thumb) : ?>
<p><a href="<?php the_permalink(); ?>"><?php echo $thumb; ?></a></p>
<?php endif; : ?>
szub_post_image() to assign thumbnail of the image attachment to $thumb (‘display=0′ turns off echo), and if it exists makes it a permalink to the post.
post-image‘ (or rather, whatever is set for the ‘customkey’ parameter) somewhere in the title field for the image upload; this text is parsed from the title attribute. Also dealt with a language localization issue for the ‘thumbnail’ filename. And implemented pseudo-caching on multi-post queries (just one query!), taking care of one of two major annoyances I’ve had with the plugin. Still extant is invalid image urls if/when a blog is moved.
szub_post_image()); post image attachment and/or ‘default_image’ can now be overridden for individual posts through a custom field (default ‘customkey’ is post-image); when ‘img_tag’ is set to
1) and ‘display’
0), Post Image now returns complete <img> element (previously it returned only the image’s url). Tentative support for WordPress 2.1 has also been shoehorned in.