Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My website has a theme that uses the preview images in the post overview pages. However these images are neither automatically selected nor did I select an image in the past.

However, even if I try to select an image. For example within a page that only shows a gallery such as this one I can not select any image for the nextgen gallery. The only images that I see are the ones directly within /files.

Which were uploaded with the media library (which I dislike because it puts all files in a flat directory structure).

My gallery files are in the sub folder (and its subfolders) /files/gallery.

So how can I use gallery images for a post preview?

share|improve this question
    
Have you tried this answer: stackoverflow.com/questions/15703189/… It won't use NextGEN, but you will be able to organize files in media library. –  Krzysiek Dróżdż Jul 31 '13 at 5:05
add comment

1 Answer

up vote 1 down vote accepted
+50

EDIT: function and plugin below work, but are not necessary

Last version of Nexgen galley as a function to set featured images:

enter image description here


EDIT 2: My plugin

Just for the exercise, I used the function originally published to create a plugin with the functionality required by OP.

The code is in a Gist.


First of all, in all the posts you want show the preview image, you have to add a custom field, named 'gallery_id':

Please note that you are not required to actually put a NexGEN gallery into the post, you can use an image from a gallery even if no gallery is attached to post, just put the gallery_id custom field in the post and you are done.

After that, in your functions.php put:

function nextgen_preview_img( $post = null, $linkto = 'post') {
  if ( intval($post) ) $postid = $post;
  if ( empty($post) ) global $post;
  if ( is_object($post) && ! isset($postid) ) $postid = $post->ID;
  if ( ! intval($postid) ) return;
  $gallery_id = (int)get_post_meta($postid, 'gallery_id', true);
  if (! intval($gallery_id) ) return;
  global $wpdb;
  $galleries = $wpdb->prefix . 'ngg_gallery';
  $images = $wpdb->prefix . 'ngg_pictures';
  $gallery = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $galleries WHERE gid = %d", $gallery_id) );
  if ( $gallery ) {
    if ( intval($gallery->previewpic) ) {
      $thumb = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $images WHERE pid = %d", $gallery->previewpic) );
    } else {
      $thumb = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $images WHERE galleryid = %d ORDER BY sortorder ASC LIMIT 1", $gallery_id) );
    }
    if ( $thumb ) {
      $meta = (array) maybe_unserialize($thumb->meta_data);
      $file = ( isset($meta['thumbnail']['filename']) ) ? 'thumbs/' . $meta['thumbnail']['filename'] : $thumb->filename;
    }
    $url = site_url( trailingslashit($gallery->path) . $file );
    $img = sprintf('<img src="%s" class="nextegen_preview_img" alt="%s" />', esc_url($url), $thumb->alttext );
    $link = $linkto == 'post' ? get_permalink($post) : site_url( trailingslashit($gallery->path) . $thumb->filename );
    printf('<a href="%s" class="nextegen_preview_a">%s</a>', $link, $img);
  }
}

This function get the preview image from the NexGEN gallery identified by the custom field 'gallery_id' attached to a post.

A gallery have some posts, so, how does the function select the image to display? It's easy: if in the NexGEN gallery setting you have selected a 'Preview image' it will be used, otherwise is used the first image, according to the sort order you set in NexGEN settings.

share|improve this answer
    
As far as I understand your answer, you suggest to modify the code of wordpress and of the template in order to achieve this? The wordpress changes would get lost with the next update and the template is commercial and all updates would as well get lost with the next update. So my understanding is that basically my request is currently not supported by wordpress or any combination with a plugin. –  Matthias Pospiech Aug 1 '13 at 16:40
    
You can create a plugin by youreself with the function or, much easier, create a child theme. –  G. M. Aug 1 '13 at 16:45
    
@MatthiasPospiech see the edit, now NexGen support post thumbnails –  G. M. Aug 1 '13 at 19:09
    
I have not tested it yet (waiting for the next service pack of ngg), but I appreciate that you have the time to make a package for the answer. –  Matthias Pospiech Aug 3 '13 at 6:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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