Is there a way to split up the post content and the gallery short code. I want to display the gallery outside my normal content no matter how or where it is placed. I can use this to get the shortcode itself:
if(has_shortcode(get_the_content(), 'gallery')){
$pattern = get_shortcode_regex();
preg_match("/$pattern/s", get_the_content(), $matches);
echo do_shortcode($matches[0]);
}
But this doesn’t work if the gallery short code isn’t the first instance. Is there a way to split my content and gallery up completely?
Edit: I have a semi-solution but it seems like a long winded way to go about it. It first grabs the first shortcode in the post (which needs to be fixed since I only want the “Gallery” shortcode) then removes all shortcodes from the content (again, not really what I want to do.
<?php if(has_shortcode(get_the_content(), 'gallery')) : ?>
<?php
$pattern = get_shortcode_regex();
preg_match("/$pattern/s", get_the_content(), $matches);
?>
<div id="content">
<?php echo strip_shortcodes(get_the_content()); ?>
</div>
<div id="gallery">
<?php echo do_shortcode($matches[0]); ?>
</div>
<?php endif; ?>
Edit #2 – Ok, I’ve been able to only get gallery shortcodes in the post. I’ve also added a filter to remove the gallery shortcode form the_content()
– the problem is that its not necessarily removing the shortcode since it does post it, but its not allowing me to run “do_shortcode()”
Functions.php
function remove_gallery($content) {
global $post;
if($post->post_type == 'artcpt')
remove_shortcode('gallery', $content);
return $content;
}
add_filter( 'the_content', 'remove_gallery', 6);
The Loop
<?php preg_match('/\]+\]/', get_the_content(), $matches); ?>
<div id="content">
<?php the_content(); ?>
</div>
<div id="gallery">
<?php echo do_shortcode($matches[0]); ?>
</div>
in The Loop it will return my short code Twice (i’m on a single page, should be looped twice – so its not running do_shortcode()). Not sure why.
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:
<?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
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( false !== $pos ) {
return substr_replace( $content, '', $pos, strlen( $shortcode[0] ) );
}
}
}
}
return $content;
}
Resources:
Stackoverflow:
- WordPress remove shortcode and save for use elsewhere
What I was originally going off of, which didn’t work as expected:
- WordPress remove shortcode from content