Originally published at
Thirzah. You can comment here or
there.
Some Trees in Sweden
Bright sun on a winter’s day
The winter sun in Sweden is bright but low, meaning low shadows and some fantastic contrasts. This row of trees lines a road alongside a viaduct, used to power the local mill.
I have discovered something quite marvelous, that I really, REALLY wish I’d discovered about 2 months ago.
History
A client was using the Livejournal Crossposter from
http://code.google.com/p/ljxp/ The theme I was using, used featured images in various page/post templates.
When the post was crossposted, however, only the_content was sent to livejournal.
The Bodge
After some swearing and cursing, I came up with a solution whereby I pulled the first inline picture in the_content, and displayed it in the theme *like* a featured post. This meant the plugin still crossposted it, but the website still kept some consistency. In retrospect, this was hugely overcomplicated. I’ll post what I did elsewhere, in case someone else needs to extract images (no idea why, mind).
The minor irritation
Having a non-premium account to test with, I found that css classes were being stripped from the img tags when I tested everything out, meaning I had no control over layout.
There had to be a better way, I thought.
And there was. But I was so focussed on the livejournal end of things, I didn’t even consider it.
The revelation
It’s blummin simple. I feel a bit of an idiot, really.
Instead of slotting in the featured image via the theme templates, I can plug it in via a filter hook in the functions file.
Even better, I can wrap it in a tag, that I can then style with css
Well, you just want the details, maybe:
The code
function put_featured_pic_in_content($content) {
global $post;
$thumbid = get_post_thumbnail_id( $post->ID);
if ($thumbid) {
$thumbarray = wp_get_attachment( $thumbid);
$attr = array('class' => "wp-post-image",);
if (is_array( $thumbarray )) {
$thumbstr = "";
$thumbstr .= '
';
$thumbstr .= get_the_post_thumbnail($post->ID,'medium', $attr );
$thumbstr .= '';
$thumbstr .= "" . $thumbarray['title'] . "
";
$thumbstr .= "" . $thumbarray['caption'] . "
";
$thumbstr .= $thumbarray['description'] ;
$thumbstr .= "";
$content = $thumbstr . $content;
}
}
return $content;
}
add_filter('the_content', 'put_featured_pic_in_content', 0); // acts as soon as it can.
There is an accompanying function,
from
http://wordpress.org/extend/ideas/topic/functions-to-get-an-attachments-caption-title-alt-description function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
add_filter('img_caption_shortcode', 'my_img_caption_shortcode_filter',10,3);