Last active
August 29, 2015 14:04
-
-
Save chriskoelle/ed5e688766303fa5ba9e to your computer and use it in GitHub Desktop.
Extract First Gallery from a post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Extract the first gallery from a page/post | |
function first_gallery_401($id = false, $echo = true) { | |
global $post; | |
if(!$id) $id = $post->ID; | |
if ( $gallery = get_post_gallery() ) : | |
add_filter( 'post_gallery', 'remove_the_first_gallery' ); | |
if($echo): | |
echo $gallery; | |
else: | |
return $gallery; | |
endif; | |
endif; | |
return false; | |
} | |
// Remove the gallery from the post | |
function remove_the_first_gallery( $output, $attr ) { | |
// We only want to remove the first gallery so let's turn off this filter | |
remove_filter( 'post_gallery', 'remove_the_first_gallery' ); | |
$output = '<!-- gallery 1 was here -->'; // Must be non-empty. | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment