Advertisement
Guest User

Untitled

a guest
Apr 11th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. /**
  2. * Gets gallery attachments from post content
  3. *
  4. * @param int $post Post ID or object.
  5. * @return mixed False on failure, array with attachment objects on succes
  6. */
  7. function get_gallery_attachments( $post = null ) {
  8.  
  9. $post = get_post( $post );
  10. if ( !$post )
  11. return false;
  12.  
  13. $gallery_attachments = array();
  14. $pattern = get_shortcode_regex();
  15. preg_match_all( "/$pattern/s", $post->post_content , $matches, PREG_SET_ORDER );
  16.  
  17. if ( !empty( $matches ) ) {
  18. foreach ( $matches as $match ) {
  19. if ( $match[2] == 'gallery' ) {
  20. // allow [[gallery]] syntax for escaping a tag
  21. if ( !( $match[1] == '[' && $match[6] == ']' ) ) {
  22.  
  23. $attr = shortcode_parse_atts( $match[3] );
  24.  
  25. if ( ! empty( $attr['ids'] ) ) {
  26. // 'ids' is explicitly ordered, unless you specify otherwise.
  27. if ( empty( $attr['orderby'] ) )
  28. $attr['orderby'] = 'post__in';
  29. $attr['include'] = $attr['ids'];
  30. }
  31.  
  32. if ( isset( $attr['orderby'] ) ) {
  33. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  34. if ( !$attr['orderby'] )
  35. unset( $attr['orderby'] );
  36. }
  37.  
  38. $defaults = array(
  39. 'order' => 'ASC',
  40. 'orderby' => 'menu_order ID',
  41. 'id' => $post->ID,
  42. 'include' => '',
  43. 'exclude' => ''
  44. );
  45. $args = wp_parse_args( $attr, $defaults );
  46. extract( $args );
  47. $id = intval( $id );
  48. if ( 'RAND' == $order )
  49. $orderby = 'none';
  50.  
  51. if ( !empty( $include ) ) {
  52. $_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
  53. $attachments = array();
  54. foreach ( $_attachments as $key => $val ) {
  55. $attachments[$val->ID] = $_attachments[$key];
  56. }
  57. } elseif ( !empty( $exclude ) ) {
  58. $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
  59. } else {
  60. $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
  61. }
  62.  
  63. if ( !empty( $attachments ) )
  64. $gallery_attachments[] = $attachments;
  65. }
  66. }
  67. }
  68. }
  69.  
  70. if ( !empty( $gallery_attachments ) )
  71. return $gallery_attachments;
  72. else
  73. return false;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement