Open to anybody who can simplify this but here’s what I came up with that worked for me. First thing’s first – get the gallery, using get_post_gallery()
, as soon as the loop starts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php if( have_posts() ) : ?> <?php while( have_posts() ) : the_post(); $gallery = get_post_gallery(); $content = strip_shortcode_gallery( get_the_content() ); ?> <div id="content"> <?php echo $content; ?> </div> <!-- id="content" --> <div id="gallery"> <?php echo $gallery; ?> </div> <!-- id="gallery" --> <?php endwhile; ?> <?php endif; ?> |
strip_shortcode_gallery()
Function – functions.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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( false !== $pos ) { return substr_replace( $content, '', $pos, strlen( $shortcode[0] ) ); } } } } return $content; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.