0

I have this code which takes the first gallery for a post's content, and formats it with more code preparing it for slideshow usage with css and js.

  $gallery = get_post_gallery_images( $post );
  $image_list = '<ul class="slideshow">';

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

  // Prepend our image list to the content of our post
  $content = $image_list . $content;
  // or return only image list
  // $content = $image_list;

  return $content;

It does the job, But how do i remove the gallery code from the content now? So i can use the rest of the content and wrap + style it.

i tried preg_replace solution in Split Content and Gallery and similar questions, but i get a php error for array to string conversion.

4
  • Do you mean that you want to remove the gallery from displaying twice? (Once in the slider and once in the post content)
    – Jami Gibbs
    Jul 4, 2015 at 17:24
  • not sure which slider you mean. i grabbed the gallery image links from the content, and i am applying new wrapping for them. Now i want to remove the tag from the content so it wont render again. basically i want to be able to control wrapping for the gallery , and the rest of the content.
    – Adeerlike
    Jul 4, 2015 at 17:38
  • Have you considered using CSS display: none applied to the first instance of the gallery?
    – Jami Gibbs
    Jul 4, 2015 at 18:48
  • very briefly, and decided not to as it is not up to par with the level i am trying to ship this at.
    – Adeerlike
    Jul 5, 2015 at 6:08

1 Answer 1

0

i used a function from @HOWDY_MCGEE 's answer plus some rewrites and attahced is my working code.

functions.php:

 function pw_show_gallery_image_urls() {

  global $post;

  // Make sure the post has a gallery in it
  if( ! has_shortcode( $post->post_content, 'gallery' ) )
    // return $content;
    return 'nadagif';
  // Retrieve the first gallery in the post
  $gallery = get_post_gallery_images( $post );

  $image_list = '<ul class="slideshow">';

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


  return $image_list;
 }

function  strip_shortcode_gallery( $content ) {
    preg_match_all( '/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER );
    if ( ! empty( $matches ) ) {
        foreach ( $matches as $shortcode ) {
            if ( 'gallery' === $shortcode[2] ) {
                $pos = strpos( $content, $shortcode[0] );
                if ($pos !== false)
                    return substr_replace( $content, '', $pos, strlen($shortcode[0]) );
            }
        }
    }
    return $content;
}

function pw_content_with_gallery_list() {

  global $post;

  // 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 class="slideshow">';

  // 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 = strip_shortcode_gallery(get_the_content());
  $content = $image_list . $content;
  return $content;
}

and then in format.php, i can do things like:

        <section class="entry-images  cf" itemprop="articleBody">
          <?php echo pw_show_gallery_image_urls(); ?>

        </section> <?php // end images section ?>

and

          <section class='entry-content'>
            <?php the_content(); ?>
          </section>

if anyone thinks of better solutions i'd be glad to learn about them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.