Does anyone know how to create a "loop" of all photos in a post?
Thanks :)
Does anyone know how to create a "loop" of all photos in a post?
Thanks :)
Look at the first example.
http://codex.wordpress.org/Function_Reference/get_children#Examples
You could use a plugin for this, which is much simpler:
- http://wordpress.org/extend/plugins/get-the-image/
reuben,
(my post has 2 images)
When I use that code within the loop on my single.php, I get 4 images that have nothing to do with this post.
Any ideas?
pavvy,
I am not sure that plugin returns an array of the images
If you have something like this, you'll get all the images
$images =& get_children( 'post_type=attachment&post_mime_type=image' );
add post_parent parameter
yup, I just figured that out (didn't have the id). Now, I'm trying to get it to give me just the url, rather than printing it to the screen. I have:
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID() );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_link($attachment_id, $size = 'thumbnail', $permalink = true, $icon = false);
}
}
but it's still adding the image tag to the page, rather than echoing the url
Change this
echo wp_get_attachment_link($attachment_id, $size = 'thumbnail', $permalink = false, $icon = false);
to
echo wp_get_attachment_link($attachment_id, 'thumbnail', false, false);
and clear the cache then test the page.
when I do that, it actually still "prints" the thumbnails to the screen.
I cleared the cache, did a forced refresh and no luck. I now have this:
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID() );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_link($attachment_id, 'thumbnail', false, false);
}
}got it to work with this (I know it's not elegant):
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID() );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
echo $image[0];
}
}
hmm....site url??
unfortunately, I can't show you the site, but it's a theme I'm working with and it's using the theme test plugin, so you can't see it without being logged it.
thank you very much for the help
This topic has been closed to new replies.