0
votes

I'm displaying latest 10 images uploaded by the displayed users in their respective profile page (buddypress.. I'm using this code to pull the images:

$media = get_posts(array(

'author' => bp_displayed_user_id(),

'post_type' => 'attachment',

'numberposts' => 10,

'post_mime_type' => 'image/jpeg' ));

foreach ($media as $image) {

list($src) = wp_get_attachment_image_src( $image->ID, 'thumbnail');

echo 'img src="'.$src.'" >';}

They work fine but here I'm unable to link the images to respective blog post.. They are simply showing the images without link attached to it.. I want to link all those images to it's parents blog post..

Thanks.. Your volunteered help will be highly appreciated..

1

1 Answers

0
votes

try the code below.

$media = get_posts(array(

    'author' => bp_displayed_user_id(),

    'post_type' => 'attachment',

    'numberposts' => 10,

    'post_mime_type' => 'image/jpeg' ));

    foreach ($media as $image) {

    list($src) = wp_get_attachment_image_src( $image->ID, 'thumbnail');
   #To add full image in the link

    list($fullImage) = wp_get_attachment_image_src( $image->ID, 'full');

    $parent = get_post_ancestors( $image->ID ); #Fetching the parent of the attachment

    $permalink = get_permalink($parent[0]); #Getting the permalink of the parent.


    echo '<a href="'.$fullImage.'"><img src="'.$src.'" ></a>';}

Source, Codex

Enjoy. Happy Coding.