Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Post attachments not working with TimThumb on Multisite WordPress

  • SOLVED

I have timthumb multisite support for my theme already working for post thumbnails but in my single.php I am pulling in all post image attachments with a foreach and wp_get_attachment_image_src() to make a custom gallery slideshow of all attachments.

Here is the working code I'm using to add multisite support for thumbnails...
$source=get_dt_image('image', $post->ID);
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode($current_blog . '/files/', $source);
if (isset($imageParts[1])) {
$source = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}


How can I do the same for my single post attachment slideshow? Here's the code for that...
<?php

$id = get_the_id();
$attachments = get_children(array(
'post_parent' => $id,
'numberposts' => 50,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'DESC',
'orderby' => 'menu_orderdate')
);

if ( !empty($attachments) ) :

$counter = 0;
$photo_thumb = '';

$cropfrom=array(
"dt_topcenter" => "t",
"dt_topleft" => "tl",
"dt_middle" => "c",
"dt_middleleft" => "l"
);
$the_crop = $cropfrom[get_option('dt_image_crop')];

foreach ( $attachments as $att_id => $attachment ) {
$counter++;

// Caption text
$caption = get_the_title();
if ($attachment->post_excerpt != "")
$caption = $attachment->post_excerpt;

$src = wp_get_attachment_image_src($att_id, 'full', true);

$photo_large .= '<a href="'. $src[0] .'" class="full-link" title="'. $caption .'"><img class="thumbnail" src="'. get_bloginfo(template_url).'/library/scripts/thumb/thumb.php?src='. $src[0] .'&w=580&zc=1&a='. $the_crop .'" alt="'. $caption .'" /></a>';
$photo_thumb .= '<li><a href="'. $src[0] .'" class="full-link" title="'. $caption .'"><img src="'. get_bloginfo(template_url).'/library/scripts/thumb/thumb.php?src='. $src[0] .'&w=120&h=90&zc=1&a='. $the_crop .'" alt="'. $caption .'" /></a></li>';
}
endif;

?>

Answers (2)

2011-04-12

AdamGold answers:

Try something like this:

<?php

global $blog_id;

$id = get_the_id();

$attachments = get_children(array(

'post_parent' => $id,

'numberposts' => 50,

'post_type' => 'attachment',

'post_mime_type' => 'image',

'order' => 'DESC',

'orderby' => 'menu_orderdate')

);



if ( !empty($attachments) ) :



$counter = 0;

$photo_thumb = '';



$cropfrom=array(

"dt_topcenter" => "t",

"dt_topleft" => "tl",

"dt_middle" => "c",

"dt_middleleft" => "l"

);

$the_crop = $cropfrom[get_option('dt_image_crop')];



foreach ( $attachments as $att_id => $attachment ) {

$counter++;



// Caption text

$caption = get_the_title();

if ($attachment->post_excerpt != "")

$caption = $attachment->post_excerpt;



$src = wp_get_attachment_image_src($att_id, 'full', true);
$src[0] = str_replace();

$src[0] = str_replace( get_bloginfo('wpurl'), '', $src[0] );
$src[0] = '/wp-content/blogs.dir/' . $blog_id . '' . $src[0];

$photo_large .= '<a href="'. $src[0] .'" class="full-link" title="'. $caption .'"><img class="thumbnail" src="'. get_bloginfo('template_url').'/library/scripts/thumb/thumb.php?src='. $src[0] .'&w=580&zc=1&a='. $the_crop .'" alt="'. $caption .'" /></a>';

$photo_thumb .= '<li><a href="'. $src[0] .'" class="full-link" title="'. $caption .'"><img src="'. get_bloginfo('template_url').'/library/scripts/thumb/thumb.php?src='. $src[0] .'&w=120&h=90&zc=1&a='. $the_crop .'" alt="'. $caption .'" /></a></li>';

}

endif;



?>

It works in my theme but I am not sure it will work in yours because my timthumb is placed in the theme directory, without any sub folders. So if it won't work please post the URL here so I can debug and see how I can get the image exactly.


Joe Calithia comments:

Thanks Adam, I actually already got it working with this...
if (isset($blog_id) && $blog_id > 0) {
$root = get_bloginfo('url');
$source = str_replace($root,'/blogs.dir/' . $blog_id . '',$source);
}


Since you pretty much came up with the same thing you get the money. Thanks again!

2011-04-12

Julian Lannigan answers:

Joe,

In your foreach, I notice a few things that might be throwing the output off.

1.
$caption = get_the_title();
Your going to want to pass the ID of the attachment "post" to this function, otherwise it will try to grab the title from the $post global. So it should be:
$caption = get_the_title($attachment->ID);

2.
$src = wp_get_attachment_image_src($att_id, 'full', true);
Should be:
$src = wp_get_attachment_image_src($attachment->ID, 'full', true);
The array that you are passing to the foreach is just an array of objects (each array item is an object and the key of that item is effective useless in this application). The key is not the attachment ID.

I think that if you make those changes, it will fix the problem.

Julian


Julian Lannigan comments:

I just noticed this also:

Anywhere you have:
get_bloginfo(template_url)

It needs to be:
get_bloginfo("template_url")

You must pass a string to the get_bloginfo function.


Joe Calithia comments:

Julian, thanks you for the help but the $att_id is correct because it's set here...
foreach ( $attachments as $att_id => $attachment ) {

Either way the code I'm using works perfectly except when used on multisite. So really all that I'm trying to do is implement the same multisite fix I used for the custom field thumbnails with the post attachments.

I need to get the following if multisite code that changes the image's src working with the wp_get_attachment_image_src()
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode($current_blog . '/files/', $source);
if (isset($imageParts[1])) {
$source = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}