0

I am trying to get the URL of the attached galley into the post on custom page layout called Gallery Page. I tried to use the code from Codex at This Page (get post gallery images). but I am not getting any thing on the page!

Can you please let me know what I am doing wrong?

<?php
/**
 * Template Name: Gallery Page
 *
 */

 if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        function pw_show_gallery_image_urls( $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 the first gallery in the post
    $gallery = get_post_gallery_images( $post );

    $image_list = '<ul>';

    // Loop through each image in each gallery
    foreach( $gallery as $image_url ) {

        $image_list .= '<li>' . '<img src="' . $image_url . '">' . '</li>';

    }

    $image_list .= '</ul>';

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

    return $content;

 }
 add_filter( 'the_content', 'pw_show_gallery_image_urls' );
    } // end while
} // end if
?>
  • First of all there are two <?php. Second it's a hook, you are suppose to add this in functions.php – Robert hue Jun 4 '15 at 3:42
  • Thanks Robert, sorry the second <?php> was a typo and I have used the hook in page before and it was working – Behseini Jun 4 '15 at 4:02
0

Your code is going to cause a fatal error as you have defined a function inside a loop, and therefore are redefining the function at every iteration thereafter. That is part of the problem. Take a look at cleaned up code:

function pw_show_gallery_image_urls( $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 the first gallery in the post
  $gallery = get_post_gallery_images( $post );

  $image_list = '<ul>';

  // Loop through each image in each gallery
  foreach( $gallery as $image_url ) {
      $image_list .= '<li>' . '<img src="' . $image_url . '">' . '</li>';
  }

  $image_list .= '</ul>';

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

  return $content;
}
add_filter( 'the_content', 'pw_show_gallery_image_urls' );

if ( have_posts() ) {
  while ( have_posts() ) {
    the_post(); 

  } // end while
} // end if

Note the Loop at the bottom. You have added a filter to the_content but there is nothing in the Loop that would apply that filter, or even print any content at all. You need:

if ( have_posts() ) {
  while ( have_posts() ) {
    the_post(); 
    the_content(); // minimum
  } // end while
} // end if
  • Thanks s_ha_dum, this is kinda working now but instead of getting images in an HTML <li> list I am getting images between <p><a> elements! plus the size are all in width="300" height="225" but I need to get the actual size. – Behseini Jun 4 '15 at 6:17

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.