0

Ok so I am using the [multigallery] function and it works fine, but one thing I want to add.

Here is the code that I am using for the multigallery in my functions.php file

function get_random_gallery_images(){ 
global $wpdb,$post; 
$ids = ""; 
$counter = 0; 
$number_of_posts = 6; 
$args = array( 
'post_type' => 'attachment', 
'numberposts' => 6, 
'post_status' => null, 
'orderby' => 'rand', 
'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; }
function multi_gallery_shortcode()
{
$attachment_ids = get_random_gallery_images();
return do_shortcode('[gallery columns="6" order="ASC" orderby="menu_order"      include="'.$attachment_ids.'" link="gallery"]');
}
add_shortcode('multigallery', 'multi_gallery_shortcode');

And here is how it looks

enter image description here

It's working Great but here are some functions that I want to add

enter image description here

I want to add the photo count to the shortcode with css ass well, and the title above the thumbnails, but the problem is, I don't know where to add the codes or the <div> codes either, can anyone help me with this problem.

Here is a code i have to call the number of photos, but where do i add this for it to show up in the [multigallery] shortcode.

<?php
$attachments = get_children(array('post_parent'=>$post->ID));
$nbImg = count($attachments);
echo '<span class="galNum">'.$nbImg.'</span> Photos.';
?>

1 Answer 1

1

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="%s">%s</a></div>', $gallery_url , $gallery_title );
    $gallery_sc = sprintf('[gallery columns="1" order="ASC" orderby="menu_order" include="%s" link="gallery"]',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; 
}
5
  • Thank you this is perfect, Also how do i add to the title this code '. get_attachment_link($attachment->ID) . ' @birgire
    – Mihad Aiko
    Commented Apr 7, 2013 at 20:50
  • and to the photo count
    – Mihad Aiko
    Commented Apr 7, 2013 at 20:56
  • I added two variables that you can play with: $gallery_title and $gallery_url, so you can add your custom title and url. Hope this helps.
    – birgire
    Commented Apr 8, 2013 at 10:53
  • Thanks, i added this to the url '. get_attachment_link($attachment->ID) . ' but it didn't work, it's suppose to grabe the attachment page url of the first image uploaded to the post, how can i fix it to work
    – Mihad Aiko
    Commented Apr 8, 2013 at 21:50
  • ok, I updated the code. It is a kind of a skeleton but I'm sure you can work it out further ;-)
    – birgire
    Commented Apr 9, 2013 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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