0

Searching in WP codex I found this function:

    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 all galleries of this post
  $galleries = get_post_galleries_images( $post );

  $image_list = '';

  // Loop through all galleries found
  foreach( $galleries as $gallery ) {

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

      $image_list .= '<div><img src="' . $image . '" ></div>';

    }

  }

  $image_list .= '';

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

  return $content;

 }
 // add_filter( 'the_content', 'pw_show_gallery_image_urls' );

In the original code the return was the image URL inside a list. I adapted a little bit for create the structure I need to create a slideshow using Slick Slider.

Now I'm trying to output this function inside my single-{slug}.php to get all the images inside the gallery wrapped in a div.

I tried:

<div class="slider">
     <?php pw_show_gallery_image_urls( get_the_content() );  ?>
</div>

And:

<div class="slider">
     <?php pw_show_gallery_image_urls( $content );  ?>
</div>

The function expects one parameter, but I don't know what.

6
  • Have you tried: <?php echo pw_show_gallery_image_urls( get_the_content() ); ?>?
    – Andy W
    Oct 4 '17 at 17:04
  • @AndyW tried now, it works, but the gallery shortcode prints too.
    – Luan
    Oct 4 '17 at 17:11
  • do_shortcode should do the trick: <?php echo do_shortcode(pw_show_galler_image_urls( get_the_content() ); ?>
    – Andy W
    Oct 4 '17 at 17:15
  • not working (also add the ')' missing).
    – Luan
    Oct 4 '17 at 17:31
  • Good call on the missing parenthesis. I was also missing a "y" in the word gallery. Hope you didn't copy and paste.
    – Andy W
    Oct 4 '17 at 18:27
0

Ok, so, I found this function, but it wasn't in the codex. It came from Pippin's Plugins, just to clarify for any future readers.

It appears this function was used as a filter added onto the content, and will only run when filtering existing content, because it's retrieving & replacing the galleries placed inside the post content.

So, I'd recommend adding the filter back on, and not using outside of the main content, here's a reworked version of the function repurposed for your slider.

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 all galleries of this post
    $galleries = get_post_galleries_images( $post );

    // Loop through all galleries found
    foreach( $galleries as $gallery ) {

        $image_list = array();

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

            $image_list[] = '<div><img src="' . $image . '" ></div>';

        }

        // Append our image list to the content of our post
        if ( !empty( $image_list ) {

            $content .= '<div class="slider">' . implode('', $image_list) . '</div>';

        }

    }

    return $content;

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

If this doesn't work the way you want it to, though, you might have to find a different function to start from, or write it from scratch.

1

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.