0

I want to get the users nickname who uploaded a media file. I'm getting all media files with this code and want to link to the users nickname / profile

<?php

            $args = array(
                'post_type' => 'attachment',
                'post_status' => null,
                'post_parent' => null, // any parent
                'numberposts' => -1,
                ); 
            $attachments = get_posts($args);
            if ($attachments) {
                foreach ($attachments as $post) {

            ?>
            <?php $author = get_the_author($post->ID); ?> 

            <a href="myurl/<?php echo $author ?>"><img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>" width="400" alt="<?php the_title(); ?>"/></a>

            <?php 

                }
            }
            ?>

FIXED

<?php

            $args = array(
                'post_type' => 'attachment',
                'post_status' => null,
                'post_parent' => null, // any parent
                'numberposts' => -1,
                ); 
            $attachments = get_posts($args);
            if ($attachments) {
                foreach ($attachments as $post) {

            ?>
            $temp_post = get_post($post_id);
        $user_id = $temp_post->post_author;
        $user_login = get_the_author_meta('user_login',$user_id);

            <a href="myurl/<?php echo $user_login ?>"><img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>" width="400" alt="<?php the_title(); ?>"/></a>

            <?php 

                }
            }
            ?>   

I'm not getting this to work. I have tried a lot of combinations, but the nickname is not getting rendered. Anyone can help me out? Thanks!

0

Got it to work

            $temp_post = get_post($post_id);
            $user_id = $temp_post->post_author;
            $user_login = get_the_author_meta('user_login',$user_id);
            echo $user_login;
  • Please explain why this helps and how it works. – kaiser Sep 28 '15 at 23:49
  • Well the problem was I needed to make sure I got the uploaders ID first and in my first try I couldn't fetch it. So I found the second solution to see who is the author (uploader of the image) and using user_login displays the nickname/userlogin. Fixed it in the question – AKNL Oct 11 '15 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.