
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.
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.
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);
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
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.
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.
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:
Here is a quick example of the functions in use:
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.
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?
Very useful functions but can you post the code which has to be inserted into the template again since it doesn
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…
Thanks Shawn for the alert. I’m uploading the files now.
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.
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
Nice. Thank you!
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...
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
you do that in your wordpress settings under Geberal > Media
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.
Nice. Thanks!
[...] Easy Peasy Images Suggestion Roundup [...]
No it will only show one. This is a replacement for the custom field technique not a replacement for the gallery shortcode.
Will this show all images attached to a post? The code looks like it does but I'm only seeing the first one.