0

Currently, the code below runs well if I use the old gallery shortcode that looks like this [gallery] . However, after Wordpress 3.5 update gallery shortcodes now include the image IDs by default that looks like [gallery ids="766,765,764,763,762,761"] .

The new shortcode with ids prevents the gallery to show. What should I do to make it compatible? Its not showing anything if I used [gallery ids="766,765,764,763,762,761"]

<div class="gallery-content">
    <?php 
    if( has_shortcode( $post->post_content, 'gallery' ) ) :   
        if ( $images = get_children(array('post_parent' => get_the_ID(),'post_type' => 'attachment','post_mime_type' => 'image', 'orderby' => 'menu_order_ID'))) :
            $image_list = '<ul class="galleryslider">'; 
            foreach( $images as $image ) :
                $attachmenturl=wp_get_attachment_url($image->ID);
                $attachmentimage=wp_get_attachment_image($image->ID, 'full' );
                $img_title = $image->post_title;
                $img_desc = $image->post_excerpt;
                $image_list .= '<li><a href="'.$attachmenturl.'" '. $lightbox .' data-title="'.$img_desc.'" title="'.$img_title.'"><div class="gallery-image-title">'.$img_title.'</div>'.$attachmentimage.'</a><div class="gallery-image-desc">'.$img_desc.'</div></li>';
            endforeach; 
            $image_list .= '</ul>';  
            echo $image_list;               
        endif;
    endif;
    ?>
</div><!-- .gallery-content -->
  • What's the idea behind your code sample? From the context I guess this is code from your theme? The code fetches all attachments which are direct descendants (childs) of the current post. This should still work in current WP versions when you upload images directly from a post context. If you want to change the default gallery markup, there's a hook post_gallery in the function gallery_shortcode() you should use. – David Jan 10 '15 at 13:30
  • Hi David, I've already tested it many times, it just wont show the gallery if I use shortcode with ids. Eg. [gallery ids="766,765,764,763,762,761"] – KC Chai Jan 10 '15 at 15:46
  • Yes, we got this already. What is that code about? What is $post ans what do you want to show? – David Jan 10 '15 at 16:40
0
+50

depend on your code if you use it as gallery shortcode i think this will work :

<div class="gallery-content">
<?php 
if( has_shortcode( $post->post_content, 'gallery' ) ) :  
    if ($ids != '') {
        $images = explode(',', $ids);
    } else {
        $images = get_children(array('post_parent' => get_the_ID(),'post_type' => 'attachment','post_mime_type' => 'image', 'orderby' => 'menu_order_ID'))
    }
    if ($images) :
        $image_list = '<ul class="galleryslider">'; 
        foreach( $images as $image ) :
            if ($ids != '') {
                $image = get_post($image);
            } 
            $attachmenturl=wp_get_attachment_url($image->ID);
            $attachmentimage=wp_get_attachment_image($image->ID, 'full' );
            $img_title = $image->post_title;
            $img_desc = $image->post_excerpt;
            $image_list .= '<li><a href="'.$attachmenturl.'" '. $lightbox .' data-title="'.$img_desc.'" title="'.$img_title.'"><div class="gallery-image-title">'.$img_title.'</div>'.$attachmentimage.'</a><div class="gallery-image-desc">'.$img_desc.'</div></li>';
        endforeach; 
        $image_list .= '</ul>';  
        echo $image_list;               
    endif;
endif;
?>

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.