Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Having Problem On Getting WP Post Gallery Images URL WordPress

I need to get the URL (actual Size) of all images of gallery associated with a post in single.php. The following code from Codex returns all images in very small size 150x150 which is not suitable for what I want

<?php
while ( have_posts() ) : the_post();
if ( get_post_gallery() ) :
$gallery = get_post_gallery( get_the_ID(), false );
foreach( $gallery['src'] AS $src )
{
?>

<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />

<?php
}
endif;
endwhile;
?>

Then I find following method which returns the images in actual size <strong>BUT</strong> it returns all images in the Galleries (Not only images within the galley of current Post)

<?php
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
print_r( $images );
if ($images) :

foreach ($images as $attachment_id => $image) :

if ( $image->ID != $thumbnail_ID ) :

$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
if ($img_alt == '') : $img_alt = $image->post_title; endif;

$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];

echo '<li>';
echo '<img src="';
echo $img_url;
echo '" alt="';
echo $img_alt;
echo '" />';
echo '</li><!--end slide-->';

endif; endforeach; endif;


so I tried to retrieve this by adding $post_id = get_the_ID(); to cede as:

<?php
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$post_id = get_the_ID();
$images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
print_r( $images );
if ($images) :

foreach ($images as $attachment_id => $image) :

if ( $image->ID != $thumbnail_ID ) :

$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
if ($img_alt == '') : $img_alt = $image->post_title; endif;

$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];

echo '<li>';
echo '<img src="';
echo $img_url;
echo '" alt="';
echo $img_alt;
echo '" />';
echo '</li><!--end slide-->';

endif; endforeach; endif;


but now I am getting only white page without any on the page! Can you please let me know why this is happening and how I can fix this? Thansk

Answers (4)

2015-01-17

Navjot Singh answers:

Use the function http://codex.wordpress.org/Function_Reference/get_post_gallery_images It is made exactly for the purpose you want.


Navjot Singh comments:

For your first issue with get_post_gallery you can use the method mentioned here: http://wordpress.stackexchange.com/a/141907


Behseini comments:

Hi Navjot, I already tried this but as I said it is not doing the job! I mean I need it in single.php but without function it is not working!

2015-01-17

Romel Apuya answers:

try this

<?php
while ( have_posts() ) : the_post();
if ( get_post_gallery() ) :
$gallery = wp_get_attachment_image_src( get_the_ID(), 'full' );

foreach( $gallery['src'] AS $src )

{
?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
<?php
}
endif;
endwhile;
?>


Behseini comments:

Hi Romel id this going to return URL? I mean in the $src?


Behseini comments:

I am getting this error
Warning: Invalid argument supplied for foreach() in C:\wamp\www\WP\wp-content\themes\twentyfifteen\single-projects.php on line 12


Romel Apuya comments:

seems i was wrong..

try this

<?php
while ( have_posts() ) : the_post();
if ( get_post_gallery() ) :
echo do_shortcode('[gallery size="full"]');
endif;
endwhile;
?>


in the single.php file


Romel Apuya comments:

or you can do this in your functions.php


function show_gallery_full_image( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post,false);
$ids = explode( ",", $galleries['ids'] );
$image_list = '<ul>';
foreach( $ids as $id ) {

$image_src = wp_get_attachment_image_src( $id, 'full' ); // returns an array

if( $image_attributes ) {
$image_list .='<li><img src="'.$image_src[0].'" width="'.$image_src[1].'" height="'.$image_src[2].'" class="my-custom-class" alt="Gallery image" />';
}
}
$image_list .= '</ul>';

// Append our image list to the content of our post
$content .= $image_list;

return $content;

}
add_filter( 'the_content', 'show_gallery_full_image' );


leaving your single.php code to the original...the unmodified one...

2015-01-17

Hariprasad Vijayan answers:

Hello,

Try this


<?php
while ( have_posts() ) : the_post();
if ( get_post_gallery() ) :
$gallery = get_post_gallery( get_the_ID(), false );
$ids = explode( ",", $gallery['ids'] );
foreach( $ids as $id ) {
$image_attributes = wp_get_attachment_image_src( $id, 'full' ); // returns an array
if( $image_attributes ) {
?>
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" class="my-custom-class" alt="Gallery image" />
<?php }
}
endif;
endwhile;
?>


Behseini comments:

Hi ,
This is still returning images on 150x150 !


Hariprasad Vijayan comments:

Updated the code. Please check.


Behseini comments:

Thanks Hariprasad, just can you please let me know what is the rule of $ids = explode( ",", $gallery['ids'] ); at this code?


Hariprasad Vijayan comments:

get_post_gallery() returns attachment id, src etc. Your previous code processing based on $gallery['src']. Here am using $gallery['ids'].
But it is string( ids are separated with comma. Check example mentioned here http://codex.wordpress.org/Function_Reference/get_post_gallery ), so am changing to an array for processing.

Thanks.

2015-01-17

Dbranes answers:

Hi, you can just use a simple filter:

add_filter( 'shortcode_atts_gallery','wpq_full_size_gallery' );
$gallery = get_post_gallery( get_the_ID(), false );


where

function wpq_full_size_gallery( $out ){
remove_filter( current_filter(), __FUNCTION__ );
$out['size']='full'; // Edit this to your needs!
return $out;
}


and you're done!


Dbranes comments:

<strong>PS:</strong>

Your second example has:

'post_parent' => $post_id,


where <em>$post_id</em> is undefined, so it searches for attachments in all posts.

---

Where you have it defined as:

$post_id = get_the_ID();

then <em>get_children()</em> is fetching all the attachments that were uploaded to that particular post.

So most likely you didn't upload any for that post,

---

You can use images from other posts in your gallery shortcode, so that's why your first example works.

---

Using the gallery filter (<em>shortcode_atts_gallery</em>) , you can save extra database queries from <em>wp_get_attachment_image_src()</em> calls when you try to fetch the full size images.