wp_get_attachments issue on gallery (post format)
-
Looking at the codex for querying all my attachments I was able to successfully create a page template (page-gallery) that accurately showed all the images from all my posts:
<div id="tile_cage" style="position:relative"> <ul id="tiles"> <?php $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => -1, 'post_status' => null, // 'post_parent' => $post->ID ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { // Method #1: Allows you to define the image size // http://stackoverflow.com/questions/10389519/wordpress-get-image-url-currently-using-wp-get-attachment-url $src = wp_get_attachment_image_src( $attachment->ID, "large"); if ($src) { // build link echo '<li><a href="' . $src[0] . '"class="fancybox"' . '>' . wp_get_attachment_image($attachment->ID, 'medium') . '</a>' . '</li>' ; } } } ?> </ul> </div><!-- tiles_cage //-->
Now I took the very same code and created a format-gallery.php which reads like this:
<div id="tile_cage" style="position:relative"> <ul id="tiles"> <?php $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { // Method #1: Allows you to define the image size // http://stackoverflow.com/questions/10389519/wordpress-get-image-url-currently-using-wp-get-attachment-url $src = wp_get_attachment_image_src( $attachment->ID, "large"); if ($src) { // build link echo '<li><a href="' . $src[0] . '"class="fancybox"' . '>' . wp_get_attachment_image($attachment->ID, 'medium') . '</a>' . '</li>' ; } } } ?> </ul> </div><!-- tiles_cage //-->
Notice the only thing I did was have the same post refer to itself
'post_parent' => $post->ID
This yields a page that doesn’t grab any of the images I queried. Source code reveals:
<ul id="tiles"> </ul> </div><!-- tiles_cage //-->
I am not sure why this has to be difficult, so I went back to the Codex and just copied its example of nabbing all the attachments of a post to the same effect. No images. I am not sure what I am doing wrong..
-
Alright scratch what I said. I have gone thru a couple dozen different sites that pretty much say this is how one grabs the attachments of a post:
global $post; // refers to the post or parent being displayed $attachements = query_posts( 'post_type' => 'attachment', // only get "attachment" type posts 'post_parent' => $post->ID, // only get attachments for current post/page 'posts_per_page' => -1 // get all attachments ); foreach($attachements as $attachment){ // Do something exceedingly fancy }
Now just to be sure I have made sure that the $post->ID is showing up by running
echo $post->ID
This correctly shows me the id of the post. (119 in this case)
However, unless I remove the post_parent parameter all together I do not see any images that are attached to that post. I am only able to either see all my attachments (all images) or none of them at all. Here is the code that I am running on format-gallery.php:
<?php echo $post->ID; ?> <?php $tile_grabs = array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'numberposts' => -1 ); $attachments = query_posts( $tile_grabs ); if ( $attachments ) { foreach ( $attachments as $attachment ) { // Method #1: Allows you to define the image size // http://stackoverflow.com/questions/10389519/wordpress-get-image-url-currently-using-wp-get-attachment-url $src = wp_get_attachment_image_src( $attachment->ID, "large"); if ($src) { // build link echo '<li><a href="' . $src[0] . '"class="fancybox"' . '>' . wp_get_attachment_image($attachment->ID, 'medium') . '</a>' . '</li>' ; } } } ?>
I have tried using the get_post_ID() parameter in the post_parent. Both successfully retrieve the exacting ID I want but no images are every shown. I am at a loss.
One thing that I want to throw into this cause it might be related is that I am using custom post formats. In this case, its called format-gallery.php.
I have my single.php file simply doing this to refer to the right format file:
<?php get_header(); ?> <?php // quick check for post format // if (have_posts()) : while (have_posts()) : the_post(); if(!get_post_format()) { get_template_part('format', 'single'); } else { get_template_part('format', get_post_format()); } endwhile; endif; ?> <?php get_footer(); ?>
Here is a link to the full code.
Here is the full code that I am using, maybe someone can help out.
- The topic ‘wp_get_attachments issue on gallery (post format)’ is closed to new replies.