Formatting post content to exclude gallery

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( "https://wordpress.stackexchange.com/". 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.

Leave a Comment