0

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.

1

Well, I found not an answer, but a workaround for this question. In the_mx_limited_gallery function, I added a negated is_single check around most of the contents of the function, so basically the limited posts and custom code show only if you are on a page other than a single post.

This isn't how I originally intended, but it works for the time being. Below is the adjusted code:

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
    $mx_colcount = the_mx_gal_colcount_switcher(); // Customizer controls
    if( get_post_format() == 'gallery' ) { // opens gallery post format check

        if( !is_single() ) { // opens non single page if statement
        // setup shortcode attributes
        $atts = shortcode_atts( array(
        ...

        } // closes non single page if statement

    } // closes gallery post format
}
add_filter( 'post_gallery', 'the_mx_limited_gallery', 10, 1 );

I forgot the add_filter part in the original question.

If anyone still can find a solution that only adjusts the shortcode attributes, I am still open for suggestions or answers. For now, I will mark this as answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.