Created
November 25, 2011 10:04
-
-
Save eteubert/1393186 to your computer and use it in GitHub Desktop.
WordPress: Link Attachments to the Post Displaying Them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if ( $attachment->post_parent > 0 ) { | |
| $link = get_permalink( $attachment->post_parent ); | |
| } else { | |
| $link = get_attachment_link( $attachment->ID ); | |
| } | |
| $img = wp_get_attachment_image( $attachment->ID ); | |
| ?> | |
| <a href="<?php echo $link ?>"><?php echo $img ?></a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $args = array( | |
| 'posts_per_page' => 5, | |
| 'post_type' => 'attachment' | |
| ); | |
| $attachments = new WP_Query( $args ); | |
| if ( $attachments->have_posts() && $attachments->post_count >= 3 ) : ?> | |
| <div class="gallery"> | |
| <div class="items"> | |
| <?php foreach ( $attachments->posts as $attachment ): | |
| if ( $attachment->post_parent > 0 ) { | |
| $link = get_permalink( $attachment->post_parent ); | |
| } else { | |
| $link = get_attachment_link( $attachment->ID ); | |
| } | |
| $img = wp_get_attachment_image( $attachment->ID, 'gallery' ); | |
| ?> | |
| <a href="<?php echo $link; ?>"><?php echo $img; ?></a> | |
| <?php endforeach; ?> | |
| </div> | |
| </div> | |
| <?php endif; ?> | |
| <?php wp_reset_query(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment