1

I created a snippet to get all attachment from a post, included attachments from comments.

<?php 

$post_attachments = get_posts( array (
    'post_type' => 'attachment',
    'posts_per_page' => -1,
    'post_parent' => $post->ID

));

?>
<ul class="attach-list-post">
<?php 
foreach ( $post_attachments as $post_attachment ) {
    $name = $post_attachment->post_title;
    $date = $post_attachment->post_date;
    echo '<li>' . wp_get_attachment_link( $post_attachment->ID, '', false, false ) . $name . $date . '</li>';
} 
?>
</ul>

It returns also the date of the attachment. Now, I need to return also the username of the user who attached the file, that can be the user who commented the post. I've tried using:

$author = $post_attachment->post_author;

but it doesn't return any kind of value.

Any suggestion on this?

0

$post_attachment->post_author will return user ID. You need to fetch user details from user ID.

get_userdata( $userid );

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.