I’ve seen so many tutorials on how to get TimThumb to work with WordPress Multisite and have found none of them really work. This tutorial will not only explain how to get TimThumb working with Multisite but also show you how to get it working with the built-in Featured Image WordPress option, Custom Fields and Image Attachments.
Featured Image
Open functions.php and add:
function get_image_path($gp_image) {
$theImageSrc = wp_get_attachment_url($gp_image);
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/', $theImageSrc);
if (isset($imageParts[1])) {
$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
return $theImageSrc;
}
Now you can use the following code inside single.php, category.php etc. to output your image URL.
<?php echo get_image_path(get_post_thumbnail_id()); ?>
You will probably want to insert this inside an image tag:
<img src="<?php get_image_path(get_post_thumbnail_id()); ?>" alt="" />
Custom Field Image
Open functions.php and add:
function get_image_path($gp_image) {
$theImageSrc = $gp_image;
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/', $theImageSrc);
if (isset($imageParts[1])) {
$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
return $theImageSrc;
}
Now you can use the following code inside single.php, category.php etc. to output your image URL, where thumbnail is the name of your custom field that contains the URL of your image.
<?php echo get_image_path(get_post_meta($post->ID, 'thumbnail', true)); ?>
You will probably want to insert this inside an image tag:
<img src="<?php echo get_image_path(get_post_meta($post->ID, 'thumbnail', true)); ?>" alt="" />
Image Attachment
Open functions.php and add:
function get_image_path($gp_image) {
$theImageSrc = $gp_image;
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/', $theImageSrc);
if (isset($imageParts[1])) {
$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
return $theImageSrc;
}
Now you can use the following code inside single.php, category.php etc. to output your image URL. This will display the first image uploaded to your post. Change the orderby and order options to change which image is outputted.
<?php $args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'numberposts' => 1, 'orderby' => menu_order, 'order' => ASC); $attachments = get_children($args);
if ($attachments) { foreach ($attachments as $attachment) { ?>
<?php echo get_image_path(wp_get_attachment_url($attachment->ID)); ?>
<?php }} ?>
You will probably want to insert this inside an image tag:
<?php $args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'numberposts' => 1, 'orderby' => menu_order, 'order' => ASC); $attachments = get_children($args);
if ($attachments) { foreach ($attachments as $attachment) { ?>
<img src="<?php echo get_image_path(wp_get_attachment_url($attachment->ID)); ?>" alt="" />
<?php }} ?>
→ This item is free to use in commercial projects, no attribution necessary.

No Comments