-
Notifications
You must be signed in to change notification settings - Fork 16
/
wp_bootstrap_gallery.php
186 lines (148 loc) · 4.66 KB
/
wp_bootstrap_gallery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Class Name: wp_bootstrap_gallery
* GitHub URI: https://github.com/twittem/wp-bootstrap-gallery
* Description: A custom Wordpress gallery for dynamic thumbnail layout using Twitter Bootstrap 2.2.2 (https://github.com/twitter/bootstrap/) thumbnail layouts.
* Version: 0.1
* Author: Edward McIntyre - @twittem
* Licence: WTFPL 2.0 (http://sam.zoy.org/wtfpl/COPYING)
*/
function wp_bootstrap_gallery( $content, $attr ) {
global $instance, $post;
$instance++;
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( ! $attr['orderby'] )
unset( $attr['orderby'] );
}
extract( shortcode_atts( array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'figure',
'icontag' => 'div',
'captiontag' => 'figcaption',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr ) );
$id = intval( $id );
if ( 'RAND' == $order ) {
$orderby = 'none';
}
if ( $include ) {
$include = preg_replace( '/[^0-9,]+/', '', $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 ( $exclude ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $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 ) ) {
return;
}
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link( $att_id, $size, true ) . "\n";
return $output;
}
$itemtag = tag_escape( $itemtag );
$captiontag = tag_escape( $captiontag );
$columns = intval( min( array( 8, $columns ) ) );
$float = (is_rtl()) ? 'right' : 'left';
$selector = "gallery-{$instance}";
$size_class = sanitize_html_class( $size );
$output = "<ul id='$selector' class='thumbnails'>";
/**
* Count number of items in $attachments array, and assign a colum layout to $span_array
* variable based on the mumber of images in the $attachments array
*/
$span_array = null;
switch (count($attachments)) {
case 1:
/* One full width image */
$span_array = array(12);
break;
case 2:
/* Two half width images */
$span_array = array(6,6);
break;
case 3:
/* One 3/4 width image with two 1/4 width images to the right */
$span_array = array(8,4,4);
break;
case 4:
/* One full width image with three 1/3 width images underneath */
$span_array = array(12,4,4,4);
break;
case 5:
/* Two half width images with fout 1/4 width images underneath */
$span_array = array(6,6,4,4,4);
break;
case 6:
/* One 2/3 width image with two 1/3 width images to the right,
* and three 1/3 width images underneath */
$span_array = array(8,4,4,4,4,4);
break;
default:
/* One full width image with two 1/2 width images underneath
* All remaining images 1/3 width underneath */
$span_array = array(12,6,6,4);
break;
}
$attachment_count = 0;
foreach ( $attachments as $id => $attachment ) {
$attachment_image = wp_get_attachment_image( $id, 'full');
$attachment_link = wp_get_attachment_link( $id, 'full', ! ( isset( $attr['link'] ) AND 'file' == $attr['link'] ) );
$output .= "<li class='span" . $span_array[$attachment_count] . "'>";
$output .= $attachment_link . "\n";
$output .= "</li>\n";
if(count($attachments) >= 7 && $attachment_count == 3){
$attachment_count = 3;
} else {
$attachment_count++;
}
}
$output .= "</ul>\n";
return $output;
}
add_filter( 'post_gallery', 'wp_bootstrap_gallery', 10, 2 );
function wp_bootstrap_gallery_add_attachment_class($html){
$postid = get_the_ID();
// Strip all CSS Classes
$html = preg_replace('/class=".*?"/', '', $html);
// Add thumbnail class to link
$html = str_replace('<a','<a class="thumbnail"',$html);
return $html;
}
add_filter('wp_get_attachment_link','wp_bootstrap_gallery_add_attachment_class',10,1);
?>