888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

January 10, 2009

My last post, Post image the easy peasy way, got a great response including a few really interesting suggestions so I have expanded it a little and rolled it all up into a single file that can be included from within a theme’s functions.php file.

There’s a lot more code in this file than in the original function so I’m not going to explain it, I will just go through the additional content and the change to the usage of the function.

New stuff

Hyperlinks

Ben Whitehouse suggested adding optional hyperlinks to the image. It is now possible to specify within the function whether the image should link to the post or not.

Thickbox

Tony Gray made some amendments to make the image expand using thickbox.

Within the file, below, is a function that automatically queues up the thickbox javascript and css. You can then use the function to choose whether to link the image to the full-size version using thickbox.

If you aren’t going to use that you might want to comment out the following line from the class constructor so it doesn’t call the css and javascript:

[php]
add_action('wp_head', array(&$this,'enqueue_thickbox'),1);

Including the image in the feed

Vivien asked about including the image in the feed. Included in the file is a hook that adds the image to the beginning of the content automatically. To do this I had to amend the function to make echoing the content optional so you can retrieve the image instead.

The image included is a thumbnail. You can change it by amending the function:add_image_to_feed

More post sizes

Cubus suggested using the Tim Thumb image resizing script. I haven’t used it here because I wanted to simply use the built in functions, but I wanted to highlight it as it is a great idea.

Default images by category

Jeff asked about default images, specifically if it was possible to assign default images to each category. It is, and now you can.

Included in the file is some extra functions that add a list of categories to the media browser so that you can assign images to one or more categories. If the function doesn’t find a post attachment with a sort order of 0 then it will default to the image attached to the category the post is in. Note: it will use the first category image it finds.

Using it

There is still only one function that matters and that is the template tag, shown below:

[php]

//in the example video the single post page uses:

Every argument within the function has a default. They are as follows:

Example video

Here is a quick example of the functions in use:

Download

You can download the post image code here. To use it include the file from inside the functions.php file, and insert the the_image tag inside the loop where you want the image to appear.

Html 5 Gallery
Wordpress html5?
6 comments
page 1305
Quick N Dirty Admin Login Screen
Login screen css?
no comment
page 128
What Wordpress Workflow Needs
Word press workflow?
3 comments
page 1226
How To Add Sidebars To A Theme
Wordpress thematic modify sidebars?
10 comments
page 1053
Post Image The Easy Peasy Way
Wordpress theimage?
26 comments
page 1065
Six Million Ways To Die Choose One
Ways to die without noticing?
14 comments
page 1128
Using Your Own Url Shortener
Htaccess tiny url shortner?
4 comments
page 1190
Photoshop Design Framework
Photoshop framework?
3 comments
page 296
Creating Custom Urls
Custom url for wordpress page?
6 comments
page 80
How To Add Sidebars To A Theme
How to add a sidebar to a theme?
10 comments
page 1053
Photoshop Design Framework
Framework in photoshop?
3 comments
page 296
What Wordpress Workflow Needs
Wordpress workflow?
3 comments
page 1226
Html 5 Gallery
Wordpress html5?
6 comments
page 1305
Using Your Own Url Shortener
Build your own url shortener?
4 comments
page 1190
My Experience Of Flexx
Cant upload image to flexx wordpress theme?
4 comments
page 1026
Post Image The Easy Peasy Way
Wpget post image?
26 comments
page 1065
Post Image The Easy Peasy Way
Get postattachments?
26 comments
page 1065
Html 5 Gallery
Html5 plugin wordpress?
6 comments
page 1305
Questions About Habari For Wordpress Users
Habari or wordpress?
6 comments
page 424
Custom Hooks For Admin Pages
Wordpress post form admin pages?
one comment
page 430
3 Ways To Speed Up Your Blog Without A Cache Plugin
Wordpress cache disqus?
one comment
page 1321
Updating Code Snippets Here
Yb fun blogs?
no comment
page 1338
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Using Wordpress As A Php Framework
Using wordpress as a framework?
2 comments
page 335
Using Your Own Url Shortener
Rewriterule shorten url?
4 comments
page 1190
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
Wordpress 25 Exif Fields
Wordpress exif plugin?
12 comments
page 230
Post Image The Easy Peasy Way
Wp image attached linkable?
26 comments
page 1065
My Experience Of Flexx
How to edit flexx theme wordpress?
4 comments
page 1026
Post Image The Easy Peasy Way
List image attachments wordpress?
26 comments
page 1065
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Wp get first image for post?
26 comments
page 1065
  1 query every 1049 seconds, updated 1 seconds ago.
Thursday, 12pm
 __
(__)
   `

 Len

When I embed a photo in a post that has a category, I get a double posting of the image, one as the embedded image, one as the category image. But when I got to the media library, there is no category marked on the image. Why?

Thursday, 12am
 __
(__)
   `

 Michael Oeser

Very useful functions but can you post the code which has to be inserted into the template again since it doesn

Tuesday, 10am
 __
(__)
   `

 shawn

Is there a variable that I can use on my homepage to display the ‘default category image’?

I’m simply looking for a variable or something that I can use within the excerpt that will force show the default image even if an image shows in the article.

It’s for a kinda special project I am working on where I have the categories set to the authors names with the default picture being a photo of the author. I wanted to use that default author photo image in the widget.

thanks

btw
I don’t really understand how the comment system replies work. Am I reading from right to left, top down, etc…

Monday, 4pm
 __
(__)
   `

 admin

Thanks Shawn for the alert. I’m uploading the files now.

Monday, 2pm
 __
(__)
   `

 shawn

BTW… the link to the download for the code is broken. I already have it from a few weeks ago, but others can’t get to it now.

Monday, 2pm
 __
(__)
   `

 shawn

Plugin works absolutely perfect!

I do have one question:

What would be the function for pulling the ‘default category image’ so that I can use that image only for the thumbnail on my homepage?

My homepage has small ‘excerpt’ boxes which show the latest posts with the thumbnail and a bit of text and readmore.

Instead of showing the article thumbnail on the homepage, I would like to replace the logic to show the image that is set as the category default if the article has no images.

Right now I use the following:

When there is no image in a post, it shows the category default. When there is an image in the post, it grabs that image instead.

I am trying to override that logic.

thanks

Tuesday, 1am
 __
(__)
   `

 Rahman

Nice. Thank you!

Friday, 10am
 __
(__)
   `

 Andrew Rickmann

Dan, the functions within the loop depend on the $post global variable being set. If it is set then the function should work. However, the usual way is to create a new loop containing the posts you want, and then use it as normal.

This page should help: http://codex.wordpress.org/The_Loop#Multiple_Lo...

Thursday, 9pm
 __
(__)
   `

 danjothebanjo

Hi Andrew, this code looks like just the ticket for me. Being new to WordPress I've found it frustrating how “buried” all this access to post images is – and had no joy on the WP support forums.

How can I use your the-image tag outside of the loop? e.g. to display the first image attached to a specific post on my static homepage. I'm currently using a custom-field but I'd prefer to create a unique post to manage this image element on my homepage – it'll bjust be much easier for my non-techy client to manage!

Thanks for generously posting your code.

Dan

Thursday, 8am
 __
(__)
   `

 tom (dB)

you do that in your wordpress settings under Geberal > Media

Thursday, 2am
 __
(__)
   `

 Danno

Question if I may … how does one change the dimensions at which the images are displayed? I'm particularly interested in changing the dimensions of 'medium'.

Thanks in advance.

Thursday, 2am
 __
(__)
   `

 Danno

Nice. Thanks!