Forums

Post ALL images from a post at the end of the post automatically (2 posts)

  1. serialboxhpc
    Member
    Posted 1 year ago #

    I would like to automatically display all images that belong to a post automatically at the end of the content on single.php

    I found this code which was written for wp 2.7 and it works partially. Hoping someone can point me in the right direction or post an alternative way of doing this.

    <?php
    $post_parent = get_post($post->ID, ARRAY_A);
    $parent = $post_parent['post_parent'];
    
    $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
    foreach($attachments as $id => $attachment) :
    	echo wp_get_attachment_link($id, 'thumbnail', true);
    endforeach;
    ?>

    The above code works but instead of only showing images/attachments for the specific post, it's displaying ALL images that have been uploaded to the entire WordPress site below every post.

  2. elfin
    Moderator
    Posted 1 year ago #

    Couldn't you just use [gallery] ?

Topic Closed

This topic has been closed to new replies.

About this Topic