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

Easy Code, Question Revised WordPress

I am want TimThumb for my post attachments in an excerpt, but it is not working.

How TimThumb is called:
<img src="<?php echo get_template_directory_uri(); ?>/scripts/timthumb.php?src=<strong>IMAGEURL</strong>&amp;h=260&amp;w=260" class="index" alt="<?php the_title(); ?>" title="<?php the_title(); ?>, <?php comments_number('No comments','One comment','% comments'); ?>" />

<?php post_attachments(); ?> works on its own, but when used in place of IMAGEURL it doesn't work through TimThumb. I need you to simply make it work for my attachments.

I'm sure this is going to be an easy few bucks for somebody.

Here is my page code:
<?php get_header(); ?>

<div id="container">
<div id="sidebar_content">

<!-- Grab posts -->
<?php
query_posts('posts_per_page=3&paged='.$paged);
if (have_posts()) :
?>

<?php while (have_posts()) : the_post(); ?>

<!-- Begin excerpt wrap -->
<div class="excerpt_wrap">
<div class="excerpt_line">

<!-- Excerpt and excerpt image-->
<div class="index">
<!-- Excerpt: image -->


<a href="<?php the_permalink('') ?>">


<?php get_the_excerpt() ?>
</a>

<!-- Excerpt: post time and author -->
<div class="sub-title">
<?php the_time("F j, Y"); ?> &nbsp;/&nbsp; <?php the_author_posts_link(); ?> &nbsp;/&nbsp; <?php the_category(', ') ?>
</div>

<!-- Excerpt: post title -->
<div class="index_title">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</div>

<!-- Excerpt: words -->
<?php the_excerpt(); ?>

</div>

<!-- End excerpt wrap -->
</div>
</div>

<?php endwhile; ?>

<!-- Next/Previous entries -->
<div class="mp_archive">
<div id="more_posts">
<div class="oe"><?php next_posts_link('&laquo; Older Entries') ?></div><div class="re"><?php previous_posts_link ('Recent Entries &raquo;') ?></div>
</div>
</div>

<?php endif; ?>

</div>
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>

Answers (3)

2011-05-07

Ivaylo Draganov answers:

This seems like a custom function. What's the output of <em>post_attachments();</em>?

If you provide that I'd be able to help you.


Lucas Wynne comments:

What do you mean by the output?

Right now it outputs URLs for images.


Ivaylo Draganov comments:

The output from <em>post_attachments()</em> won't do you any good as it gives HTML tags, while you need just the src attributes. The code given by David Navarrete seems correct. I've made a little improvment on it, put this in your <em>functions.php</em>*:

function get_all_image_from_post($postId = false) {

// determine post ID
if(false === $postId) {
global $post;
$postId = $post->ID;
}

// get all images attached to the current loop post
$attachments = get_posts(array(
'post_parent' => $postId,
'post_mime_type' => 'image',
'post_type' => 'attachment',
'post_status'=>'inherit',
'numberposts' => -1
)
);

$attachments_src = array();

// get the url for each image
foreach ($attachments as $attachment){

$image_src = wp_get_attachment_image_src($attachment, 'full');
$attachments_src[] = $image_src[0];

}

// return an array of all urls
return $attachments_src;

}



And then put this within the loop** in your template:

<?php
// call the function in the loop of your template
$images = get_all_image_from_post();

// pass each image src to TimThumb
foreach ($images as $image) { ?>

<img src="<?php echo get_template_directory_uri(); ?>/scripts/timthumb.php?src=<?php echo $image; ?>&amp;h=260&amp;w=260" class="index" alt="<?php the_title_attribute(); ?>" title="<?php the_title_attribute(); ?>" />

<?php } ?>


* functions.php lives inside your theme directory, create it if it doesn't exist
** the loop is the part between <em>while:</em> and <em>endwhile;</em>

Good luck :-)


Lucas Wynne comments:

The image in the post is called using <?php post_attachments(); ?>, could this be preventing it from showing up with this code - because it's still not working?

Simply calling <?php post_attachments(); ?> actually does bring up the image in the excerpts, but I can't run it through TimThumb.


Ivaylo Draganov comments:

You can't run the image produced by <em>post_attachments()</em>, because it's a full HTML <img> tag, whereas for TimThumb to work it has to be passed an url to the image. Just try with the function that I've mentioned above. It will get all the images attached to each post and pass them to TimThumb.


Lucas Wynne comments:

I haved tried it, it's correctly outputting TimThumb but isn't adding a location of an image to it. For example, it's calling up: http://kaiio.com/wp-content/themes/Frame%20Work/scripts/timthumb.php?src=&h=260&w=260

Nothing behind the SRC :-(


Ivaylo Draganov comments:

Excuse me, there was an error in the code. Change this line:

$image_src = wp_get_attachment_image_src($attachment, 'full');


to read:


$image_src = wp_get_attachment_image_src($attachment->ID, 'full');


and you should get the image ulrs. I've tested it and it works.

2011-05-07

Maor Barazany answers:

Try this -


<?php if (has_post_thumbnail()) {
$template_directory = get_bloginfo('template_directory');
$thumb_id = get_post_thumbnail_id($post->ID);
$thumb = wp_get_attachment_image_src($thumb_id);?>
<a href="<?php the_permalink();?>" title="<?php the_title();?>">
<img src="<?php echo $template_directory.'/scripts/timthumb.php?src='.$thumb[0].'&w=260&h=260&zc=1';?>" / alt="<?php the_title();?>"></a>
<?php } ?>


Lucas Wynne comments:

This doesn't work. I'm calling post attachments, not images which have been preset as post thumbnails. :-(


Maor Barazany comments:

It is the same, so just use like this -


<?php

$template_directory = get_bloginfo('template_directory');
$thumb = wp_get_attachment_image_src($YOUR_ATTACHMENT_ID);?>
<a href="<?php the_permalink();?>" title="<?php the_title();?>">
<img src="<?php echo $template_directory.'/scripts/timthumb.php?src='.$thumb[0].'&w=260&h=260&zc=1';?>" / alt="<?php the_title();?>"></a>



Attachments are stored in the database as posts, so you have the attachment id and it is the same.


Lucas Wynne comments:

Not pulling up the image URL.


Maor Barazany comments:

You should pass the real image id instead of $YOUR_ATTACHMENT_ID, otherwise it of course can't work...

Good luck



Lucas Wynne comments:

These are being used in excerpts, so calling a specific ID wouldn't make any sense. I need to call IDs for each excerpt. Know of a way to do that?


Maor Barazany comments:

Anything can be done. I always write my own custom code for each purpose.
I didn't understand what you try to do, so I afraid I can't help you more right now.

2011-05-07

David Navarrete answers:

put this code on functions.php

function get_all_image_from_post()
{
global $post;

$imagenes = get_posts(array('post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_type' => 'attachment',
'exclude' => array(get_post_thumbnail_id()),
'numberposts' => -1
)
);
$listado_imagenes = array();
foreach ($imagenes as $imagen){
$image_src = wp_get_attachment_image_src($imagen, 'full');
$listado_imagenes[] = $imagen[0];
}
return $listado_imagenes;
}


then call the functions like that

$imagenes = get_all_image_from_post();
foreach ($imagenes as $imagen): ?>
<img src="<?php echo get_template_directory_uri(); ?>/scripts/timthumb.php?src=<?php echo $imagen; ?>&amp;h=260&amp;w=260" class="index" alt="<?php the_title(); ?>" title="<?php the_title(); ?>, <?php comments_number('No comments','One comment','% comments'); ?>" />

<?php endforeach; ?>




bye


Lucas Wynne comments:

I get this error: Fatal error: Cannot use object of type stdClass as array


David Navarrete comments:


function get_all_image_from_post()
{
global $post;

$imagenes = get_posts(array('post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_type' => 'attachment',
'exclude' => array(get_post_thumbnail_id()),
'numberposts' => -1
)
);
$listado_imagenes = array();
foreach ($imagenes as $imagen){
$image_src = wp_get_attachment_image_src($imagen, 'full');
$listado_imagenes[] = $image_src[0];
}
return $listado_imagenes;
}


sorry try with this.


Lucas Wynne comments:

I think this is extremely close... but it's still not there.

It is outputting the correct TimThumb structure but the location of the image is missing, for example:
http://domain.com/wp-content/themes/Theme/scripts/timthumb.php?src=&h=260&w=260

Right after SRC the image URL should appear, if I'm not mistaken.