Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Gets gallery attachments from post content
- *
- * @param int $post Post ID or object.
- * @return mixed False on failure, array with attachment objects on succes
- */
- function get_gallery_attachments( $post = null ) {
- $post = get_post( $post );
- if ( !$post )
- return false;
- $gallery_attachments = array();
- $pattern = get_shortcode_regex();
- preg_match_all( "/$pattern/s", $post->post_content , $matches, PREG_SET_ORDER );
- if ( !empty( $matches ) ) {
- foreach ( $matches as $match ) {
- if ( $match[2] == 'gallery' ) {
- // allow [[gallery]] syntax for escaping a tag
- if ( !( $match[1] == '[' && $match[6] == ']' ) ) {
- $attr = shortcode_parse_atts( $match[3] );
- if ( ! empty( $attr['ids'] ) ) {
- // 'ids' is explicitly ordered, unless you specify otherwise.
- if ( empty( $attr['orderby'] ) )
- $attr['orderby'] = 'post__in';
- $attr['include'] = $attr['ids'];
- }
- if ( isset( $attr['orderby'] ) ) {
- $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
- if ( !$attr['orderby'] )
- unset( $attr['orderby'] );
- }
- $defaults = array(
- 'order' => 'ASC',
- 'orderby' => 'menu_order ID',
- 'id' => $post->ID,
- 'include' => '',
- 'exclude' => ''
- );
- $args = wp_parse_args( $attr, $defaults );
- extract( $args );
- $id = intval( $id );
- if ( 'RAND' == $order )
- $orderby = 'none';
- if ( !empty( $include ) ) {
- $_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
- $attachments = array();
- foreach ( $_attachments as $key => $val ) {
- $attachments[$val->ID] = $_attachments[$key];
- }
- } elseif ( !empty( $exclude ) ) {
- $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
- } else {
- $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
- }
- if ( !empty( $attachments ) )
- $gallery_attachments[] = $attachments;
- }
- }
- }
- }
- if ( !empty( $gallery_attachments ) )
- return $gallery_attachments;
- else
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement