Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // http://www.gravityhelp.com/forums/topic/link-uploaded-image-to-created-post#post-76548
- // http://wordpress.stackexchange.com/a/44069
- // Chris Hajer 17 November 2012
- add_shortcode('gf_postimage', 'get_image_with_link');
- function get_image_with_link() {
- global $post;
- $all_images = get_posts( array(
- // Return just the first attachment
- 'numberposts' => 1,
- 'order'=> 'ASC',
- 'post_parent' => $post->ID,
- 'post_mime_type' => 'image',
- 'post_type' => 'attachment'
- ) );
- // Step through all image attachments
- foreach ( $all_images as $image ) {
- // Get the attachment ID
- $image_id = $image->ID;
- // Get the image alt tag
- $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
- // Get the attachment URL for the large size
- $image_info = wp_get_attachment_image_src( $image_id, 'large' );
- // Get the parent post ID
- $parent_id = $image->post_parent;
- // Get the parent post Title
- $parent_title = get_the_title( $parent_id );
- // Get the parent post permalink
- $parent_permalink = get_permalink( $parent_id );
- }
- $post_content_prepend = "<a href='$parent_permalink' title='$parent_title'><img src='{$image_info[0]}' width='{$image_info[1]}' height='{$image_info[2]}' alt='$image_alt' class='size-large alignnone wp-image-$image_id' /></a>";
- return $post_content_prepend;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement