Adding Image Count to Multigallery

You can try

function multi_gallery_shortcode($atts, $content=null) {
    extract( shortcode_atts( array(
                'pid' => 0,
        ), $atts ) );

    //format input
    $pid = intval($pid);

    // construct a post object dependent on the input value
    if($pid>0){
        // query a post object
        $pobj = get_post( $pid );
    }else{
        global $post;
        // current post object
        $pobj = &$post;
    }

    // construct gallery title
    $gallery_title = $pobj->post_title; // customize to your needs

    // construct gallery url
    $gallery_url = ""; // default first image gallery url
    $attributes = wp_get_attachment_image_src( get_first_gallery_image($pobj->ID),'thumbnail'); // customize  to your needs
    if(isset($attributes[0]))
        $gallery_url = $attributes[0];

    // format output:
    $before     = sprintf('<div class="gallery-before"><a href="https://wordpress.stackexchange.com/questions/94846/%s">%s</a></div>', $gallery_url , $gallery_title );
    $gallery_sc = sprintf('',get_random_gallery_images($pobj->ID));
    $after      = sprintf('<span class="galNum">%d</span> Photos.', get_total_attachments($pobj->ID));

    return $before.do_shortcode($gallery_sc).$after;
}
add_shortcode('multigallery', 'multi_gallery_shortcode');

where the three helper functions are:

function get_random_gallery_images($post_id){ 
    global $wpdb; 
    $ids = ""; 
    $args = array( 
        'post_type'      => 'attachment', 
        'post_mime_type' => 'image', 
        'posts_per_page' => 6, 
        'post_status'    => 'any', 
        'orderby'        => 'rand', 
        'post_parent'    => $post_id, 
    ); 
    $attachments = get_posts($args); 
    if ($attachments) { 
        $tmp=array();
        foreach ($attachments as $attachment) {
                $tmp[] = $attachment->ID; 
        }  
        $ids=implode(",",$tmp);
    } 
    return $ids; 
}

and

function get_total_attachments($post_id){
    $args=array(
        'post_parent'     => $post_id, 
        'post_mime_type'  => 'image', 
        'post_type'       => 'attachment',
    ); 
    return count(get_children( $args ));
}

and finally

function get_first_gallery_image($post_id){ 
    global $wpdb; 
    $id = 0; 
    $args = array( 
        'post_type' => 'attachment', 
        'post_mime_type' => 'image', 
        'posts_per_page' => 1, 
        'post_status' => 'any', 
        'orderby' => 'date', 
        'order' => 'DESC', 
        'post_parent' => $post_id, 
    ); 
    $attachments = get_posts($args); 
    if (isset($attachments[0])) { 
        $id = $attachments[0]->ID; 
    } 
    return $id; 
}

Leave a Comment