Wordpress get image url (currently using wp_get_attachment_url) Wordpress get image url (currently using wp_get_attachment_url) wordpress wordpress

Wordpress get image url (currently using wp_get_attachment_url)


The following code will loop through all image attachments and output the src url. Note that there are two methods shown, depending on your need.

<?php    global $post;    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => $post->ID );     $attachments = get_posts($args);    if ($attachments) {            foreach ( $attachments as $attachment ) {                    // Method #1: Allows you to define the image size                    $src = wp_get_attachment_image_src( $attachment->ID, "attached-image");                    if ($src) {echo $src[0];}                    // Method #2: Would always return the "attached-image" size                    echo $attachment->guid;            }    }?>