Hot answers tagged

24

Open to anybody who can simplify this but here's what I came up with that worked for me. First thing's first - get the gallery, using get_post_gallery(), as soon as the loop starts: <?php if( have_posts() ) : ?> <?php while( have_posts() ) : the_post(); $gallery = get_post_gallery(); $content = ...


19

Ok, I've been playing with this for a little bit and have managed to change the output of the Gallery block, with the following caveats: The preview does not match the output. I think this is possible but appears to be a bit more involved. Certain classes and markup are required in the output for the block to be able to parse the content and keep it ...


17

There is an easier solution now: function my_gallery_default_type_set_link( $settings ) { $settings['galleryDefaults']['link'] = 'file'; return $settings; } add_filter( 'media_view_settings', 'my_gallery_default_type_set_link'); They added a filter to customize this default value (and other values related to the new media upload popup) in WP 4.0 (...


15

All those complex functions can be reduced to one simple function: attachment_url_to_postid() You only need to parse the image URL to retrieve the attachment ID: $attachment_id = attachment_url_to_postid( $image_url ); echo $attachment_id; That's all you need.


14

First you have to get the images. How to get all images of a gallery is described here. WordPress uses two classes for unzipping files. The PHP bilt in ZipArchive() (usage see David Walsh). And PclZip, you can found this class in wp-admin/includes/class-pclzip.php. If you got problems with ZipArchive() try the PclZip class. Now you just to have to glue ...


12

The source for the gallery shortcode is in wp-includes/media.php. There are a couple of hooks in there that might work for you. Without knowing exactly what you want to do, it is impossible to be more specific. If that doesn't work then you can remove the shortcode: remove_shortcode('gallery'); And add another gallery shortcode with the alterations you ...


12

Answering my own question: As suggested by WebElaine I created a completely new block to fix the problem. To make this task easier, I used create-guten-block by Ahmad Awais, which is pretty easy to set up. In init.php I made sure to enqueue my JavaScript and extra CSS for the frontend. This should be straight forward. I recommend reading the Learning ...


11

Here are three methods Approach #1 It's common to wrap the output with custom HTML. We can do that by using the post_gallery filter and the gallery_shortcode() callback: /** * HTML Wrapper - Support for a custom class attribute in the native gallery shortcode */ add_filter( 'post_gallery', function( $html, $attr, $instance ) { if( isset( $attr['...


8

Put this into your page template: <?php global $nggdb; $galleries = array(); $album = $nggdb->find_album(1); foreach( $album->gallery_ids as $galleryid ){ $gallery = $nggdb->find_gallery($galleryid); $galleries[$galleryid]['title'] = $gallery->title; $galleries[$galleryid]['url'] = get_bloginfo('url') ...


8

There is a simple solution for this. Let the gallery use the 5 column grid system and using the @media-queries we can alter it for tablet and mobile. I just tested it on your given URL and it works perfect. But try to remove any custom codes you have used before since I had to remove it in order to use the following code. /* For displaying 3 columns on ...


7

WordPress doesn't make it very easy to modify aspects of the gallery shortcode. Some attributions: Thanks to user peterbra for really putting the whole thing together a year ago. Thanks to user birgire for coming up with a solution as far as adding the attribute in a meaningful way. The other option besides birgires ( as far as I know ) is to pretty much ...


6

jQuery(".gallery-icon a").fancybox().attr('data-fancybox', 'wp-gallery-fancybox'); Gives all links the same rel attribute, this way the user will be able to slide between the images.


6

I've written answer to How to paginate attachments in a secondary query as gallery?, which can be used for solving this problem. Like @wpmudev-ari said the [gallery] shortcode can be rewritten, or a new shortcode could be created. I explain below - B. - how this can be done. Additionally there is a very simple solution - A. - for the pagination of the ...


6

There is a new shortcode_atts_{$shortcode} filter in WordPress 3.6 according to Mark Jaquith. You could use the shortcode_atts_gallery filter to force the link='file' attribute: add_filter('shortcode_atts_gallery','overwrite_gallery_atts_wpse_95965',10,3); function overwrite_gallery_atts_wpse_95965($out, $pairs, $atts){ // force the link='file' gallery ...


6

Old question but still getting search traffic so for everyone else: You can accomplish what you describe by filtering the gallery shortcode attributes. Also useful if you want to change the default image size, columns, etc. function gallery_should_link_to_files($out, $pairs, $atts) { $atts = shortcode_atts( array( 'link' => 'file' ), $atts );...


6

@tfer77 has given the right answer. I am here explaining it in a more better way. You need to add size="full" attribute to the gallery shortcode in the post content like [gallery ids="836,830,829" size="full"] Now when you use $gallery = get_post_gallery_images( $post->ID ); You will get full size images. You can check the same by using the filter ...


6

Deleting images using plugin: You can use this plugin, it will search your database and look if image is inserted into any post (in content, as featured image, in any custom field, anywhere...) or as background... If image is not used anywhere it will give you option to delete it. You will get list of all images on your site not used anymore, so you can ...


6

You can override the gallery shortcode link attribute with: add_filter( 'shortcode_atts_gallery', function( $out ){ $out['link'] = 'file'; return $out; } ); This means that even if your shortcodes are: [gallery ids="1,2,3"] [gallery ids="1,2,3" link="none"] the gallery output will always be generated as if you used link="file".


5

if you set image size in function.php as a "gallery" add_image_size( 'gallery', 200, 120, true ); you can remove width and height of specific image size such as "gallery": add_filter('wp_get_attachment_link', 'remove_img_width_height', 10, 4); function remove_img_width_height( $html, $post_id, $post_image_id,$post_thumbnail) { if ($post_thumbnail=='...


5

You can hijack the shortcode handler and set the attribute to a value of your choice. Then call the native callback for this shortcode. add_shortcode( 'gallery', 'file_gallery_shortcode' ); function file_gallery_shortcode( $atts ) { $atts['link'] = 'file'; return gallery_shortcode( $atts ); }


5

You don't have to insert image into post after attaching it to this post. When you edit post, click on 'Add Media' button. Upload your image and when it's uploaded just close the 'Insert Media' dialog (use 'x' button in top right corner and not 'Insert into post' button). You can then get (and show) them with this code: <?php $images = new WP_Query(...


5

If you want get_post_gallery_images to give you full size images, you can use the following: // Use full size gallery images for the next gallery shortcode: add_filter( 'shortcode_atts_gallery', 'wpse_141896_shortcode_atts_gallery' ); // Your code: $gallery = get_post_gallery_images( $post ); foreach ($gallery as $img) { ?> <li><img src="&...


5

There is now (from version 4.0) much better solution which actually sets the default option: function my_gallery_default_type_set_link( $settings ) { $settings['galleryDefaults']['link'] = 'file'; return $settings; } add_filter( 'media_view_settings', 'my_gallery_default_type_set_link'); view: Overide Gallery Default Link to Settings


5

If you're comfortable with the complete opposite of the linked answer, you simply could use this query to fetch all posts that have an attachment and then use their IDs as the post__not_in parameter of \WP_Query: $attachment_args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', '...


5

You can try the following to clear the image attachment's title when it's inserted but not updated: /** * Empty the image attachment's title only when inserted not updated */ add_filter( 'wp_insert_attachment_data', function( $data, $postarr ) { if( empty( $postarr['ID'] ) && isset( $postarr['post_mime_type'] ) &&...


5

This is probably a good case for a global variable. Just make sure it is set to an array before you add to it and also when to attempt to output the content. Convert the array to a string before adding to content. Also, make sure the meta data exists before adding to the array -- that way you're not adding empty copyright values. GLOBAL VERSION function ...


5

In order for your images to be responsive they require CSS. The CSS will inform the image to stretch 100% of the available space and to automatically adjust the height. img.responsive { width: 100%; height: auto; } Because you don't want to apply this rule to all images, you'll need to add a class on the target image which tells it to use these ...


5

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 the ...


4

I have not a ready solution, but small hints. You get all albums from nggallery with a small sql select. This get a array with all data to the album. If you have the right fields, change the * and use only the fields, there store your data. global $wpdb; $albumlist = $wpdb->get_results("SELECT * FROM $wpdb->nggalbum ORDER BY id"); After ...


4

I had the same issue and was previously using get_children() to retrieve the top 4 images from a gallery for a given page. Here's the solution I came up with: // helper function to return first regex match function get_match( $regex, $content ) { preg_match($regex, $content, $matches); return $matches[1]; } // Extract the shortcode arguments from ...


Only top voted, non community-wiki answers of a minimum length are eligible