I created a function for a theme that limits the amount of images displayed in a gallery. This is within the Gallery post format. However, I want to limit gallery images on all pages except for the single post.
My code so far is the following (shortened with the parts that are specific to the attributes in question):
function the_mx_limited_gallery( $attr ) {
$post = get_post();
$attachment_ids = the_mx_get_limited_gallery_ids();
$link_image_to = the_mx_medialink_switcher(); // Customizer controls
if( get_post_format() == 'gallery' ) { // opens gallery post format check
...
// setup shortcode attributes
$atts = shortcode_atts( array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post ? $post->ID : 0,
'itemtag' => 'figure',
'icontag' => 'div',
'captiontag' => 'figcaption',
'columns' => 3,
'include' => $attachment_ids,
'size' => 'gallery-thumb',
'link' => $link_image_to,
), $atts );
$id = intval( $atts['id'] );
if ( ! empty( $atts['include'] ) ) {
$_attachments = get_posts( array(
'include' => $atts['include'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby'],
) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( ! empty( $atts['exclude'] ) ) {
$attachments = get_children( array(
'post_parent' => $id,
'exclude' => $atts['exclude'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby']
) );
} else {
$attachments = get_children( array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby']
) );
}
...
}
The code that retrieves the gallery IDs (the_mx_get_limited_gallery_ids) has the following code:
// Function for limiting Gallery ID length (in the Gallery post format).
// see http://www.webgurus.biz/how-to-limit-wordpress-gallery-thumbnails-in-the-loop/
function the_mx_get_limited_gallery_ids() {
global $wpdb, $post;
$ids = '';
$counter = 0;
$number_of_posts = 6;
$args = array(
'post_type' => 'attachment',
'numberposts' => 6,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_status' => null,
'post_parent' => $post->ID,
);
$attachments = get_posts($args);
if($attachments) {
foreach ($attachments as $attachment) {
if($counter != 0) {
$ids .= ','.$attachment->ID;
} else {
$ids .= $attachment->ID;
}
$counter++;
}
}
return $ids;
}
To display on the front end, I tried using just filtering shortcode_atts_gallery
and displaying with do_shortcode
, but in my testing environment, this interferes with Jetpack's gallery additions. I would like my gallery to show alongside the Jetpack gallery.
Any help would be appreciated.