I have create a custom sidebar, and want to show only posts images in that sidebar. I have add a post with some text and choose image from gallery in the content of that post. Now i want to get only image tag from post by loop.

Any suggestions?


You can either make use of get_children or get_posts or even WP_Query to get the the attached image ID's to the current post

You can then use those ID's to gt the attached image with wp_get_attachment_image()

Here is a basic idea that you can use in a widget or sidebar. (CAVEAT: Untested)

$args = array( 
    'post_type' => 'attachment', 
    'numberposts' => -1, 
    'post_status' => null, 
    'post_parent' => get_queried_object_id()

$attachments = get_posts( $args ); 

if ( $attachments ) { 

    foreach ( $attachments as $attachment ) { 

        echo '<li>'; 
        echo wp_get_attachment_image( $attachment->ID, 'full' ); 
        echo  '<p>'; echo apply_filters( 'the_title', $attachment->post_title ); 
        echo '</p></li>'; 

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.