Skip to content

Instantly share code, notes, and snippets.

@sosukeinu
Created January 29, 2013 16:03
Show Gist options
  • Save sosukeinu/4665356 to your computer and use it in GitHub Desktop.
Save sosukeinu/4665356 to your computer and use it in GitHub Desktop.
WP gallery excerpt, taking *hidden* images into account
<?php
// Check a Post for shortcode Galllery
function gallery_shortcode_exists(){
global $post;
# Check the content for an instance of [gallery] with or without arguments
$pattern = get_shortcode_regex();
if(
preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
&& array_key_exists( 2, $matches )
&& in_array( 'gallery', $matches[2] )
)
return true;
# Sourced from http://codex.wordpress.org/Function_Reference/get_shortcode_regex
}
// Add Gallery Shortcode Option to Hide Images by id, from all but Admin
add_shortcode('gallery', 'custom_gallery_function');
function custom_gallery_function($atts) {
$user = wp_get_current_user();
// if current user isn't admin, add posts to be hidden to exclude
if(!in_array('administrator', $user->roles))
$atts['exclude'] = $atts['exclude'] . ',' . $atts['hide'];
// call the wordpress shortcode function
return gallery_shortcode($atts);
}
?>
<?php if( ( is_category() || is_archive() || is_home() ) && gallery_shortcode_exists() ) { ?>
<?php # Determine if the post_content column contains the string [gallery]
# Grab the list of "hide" attribute
$regex_pattern = get_shortcode_regex();
preg_match ('/'.$regex_pattern.'/s', $post->post_content, $regex_matches);
if ($regex_matches[2] == 'gallery') :
$attribureStr = str_replace (" ", "&", trim ($regex_matches[3]));
$attribureStr = str_replace ('"', '', $attribureStr);
// Parse the attributes
$defaults = array (
'hide' => '1',
);
$attributes = wp_parse_args ($attribureStr, $defaults);
if (isset ($attributes["hide"])) :
$excludeID = get_post_thumbnail_id() . ',' . $attributes["hide"];
else :
$excludeID = get_post_thumbnail_id();
endif;
endif;
# Get the first three attachments using the posts_per_page parameter
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order'=> 'ASC',
'posts_per_page' => 3,
'post_parent' => get_the_ID(),
'exclude' => $excludeID
);
$attachments = get_children( $args );
$count = count( $attachments );
# If any attachments are returned, proceed
if( $attachments ){
# Spin cycle to collate attachment IDs
foreach( $attachments as $attachment )
$includes[] = $attachment->ID;
# Format our IDs in a comma-delimited string
$includes = implode(',', $includes);
# Inject your include argument
$shortcode = "[gallery include='$includes' link='file']"; ?>
<?php # Render the Gallery using the standard editorial input syntax
echo do_shortcode($shortcode); // echo do_shortcode($shortcode);
# Add a View More link
if( $count>=3 ){ ?>
<h3 class="ribbon"><?php echo '<a href="' . get_permalink() . '">' . __('View more', 'standard') . '</a>'; ?> </h3>
<?php } ?>
<?php }
else
_e('No attachments found and no excerpt to display', 'standard'); ?>
<?php } else { ?>
<?php the_content( __( 'Continue Reading...', 'standard' ) ); ?>
<?php } // end if/else
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment