How to get clean code for a gallery?

You can try to overwrite the gallery shortcode with: add_shortcode( ‘gallery’, ‘custom_gallery_shortcode’ ); where the shortcode callback is: /** * Overwrite the native shortcode, to modify the HTML layout. */ function custom_gallery_shortcode( $attr = array(), $content=”” ) { $attr[‘itemtag’] = “li”; $attr[‘icontag’] = “”; $attr[‘captiontag’] = “p”; // Run the native gallery shortcode callback: $html … Read more

How can I make wp default gallery responsive?

You can try using css to control the visual layout. I have tested on my dev server and this was successful. @media only screen and ( max-width: 320px ) { .gallery-item {float:left;width:50% !important;} } what we have done is set the column to 50% the total container width when viewing on devices smaller then 320px. … Read more

How to determine if a post has attached images?

You could use get_posts and search for image attachments. <?php $images = get_posts(array( ‘post_parent’ => $the_parent_to_check, // whatever this is ‘post_type’ => ‘attachment’, // attachments ‘post_mime_type’ => ‘image’, // only image attachments ‘post_status’ => ‘inherit’, // attachments have this status )); if($images) { // has images } else { // no images. 🙁 } Might … Read more

Display latest 10 galleries

Getting the latest posts with a gallery is simple: search for posts with the string ‘[gallery’. $posts = get_posts( array ( ‘s’ => ‘[gallery’ ) ); Getting the first image from each gallery is harder, because you have to render the gallery shortcode. I would not use the default handler, it does much more than … Read more

How can I show these pictures in two columns in my page?

If it’s a usable option for you, you can change the output of to use <span> tag instead of a <div>. This how you’d change the output (to be added in theme functions.php) of the Caption shortcode: // Source code from http://core.svn.wordpress.org/trunk/wp-includes/media.php add_filter( ‘img_caption_shortcode’, ‘wpse57262_cleaner_caption’, 10, 3 ); function wpse57262_cleaner_caption( $output, $attr, $content ) { … Read more

Add data attribute to a gallery link?

This solution will add the data-fancybox=”group” to gallery links produced by the default shortcode. This has been tested and works regardless of whether themes have HTML5 theme support enabled for galleries or not. The solution works by using the post_gallery filter to gain access to the gallery shortcode’s output. From there, the HTML is parsed … Read more

How to get ID of images used in gallery?

Are you writing a template? A filter in functions.php or a plugin? A straightforward method could be using get_post_gallery with the second argument set to false, so that it return the object rather than the html. if ( get_post_gallery() ) : //Get the gallery object $gallery = get_post_gallery( get_the_ID(), false ); //Form an array with … Read more