Support » Plugins » [Plugin: NextGEN Gallery] gallery for a tag AND tag(s) hack suggestion

  • hello all.

    I hope not to have reinvented the wheel, but I needed to display a gallery of pictures containing a tag AND another tag etc..

    if I’m not wrong netcgen gallery can now create a gallery of tagged images where tags work in an OR fashion. this line of code:

    nggShowGalleryTags(“paris,restaurant”);

    will display a gallery of all pictures tagged “paris” OR “restaurant”, i.e. it will return all the pictures tagged “paris” (for instance even a paris swimming pool tagged “paris, swimming-pools” or a spanish restaurant tagged “madrid, restaurant”).

    what I needed was to retrieve a gallery of restaurants in paris. well, actually it was movie posters but the above example may be more clear.

    to obtain this I added two methods to the nggTags class in the lib/tags.php file.

    with one method I obtain an array of pictures’ ID only, with the other I obtain an array of pictures objects, should I need that (for instance to create a standard gallery).

    I preferred to split IDs’ and pictures objects retrieval because this way I can also pass just the ID(s) to the nggSinglePicture() function if I don’t want a gallery made of a single thumb. nggSinglePicture() needs only the picture’s ID and not the whole picture objects, so I thought it was an overkill to use an ID to create an object from which extract the just the ID again later…

    If instead I want a standard gallery, I can pass the ID’s array to the picture objects array creation method, and then to the gallery creation function.

    here are the two methods (I hope lines don’t break too much, I’m usually generous with indent and spacing):

    /**
    	 * nggTags::find_images_id_for_tags_and()
    	 *
    	 * @param mixed $taglist
    	 * @param string $mode could be 'ASC' or 'RAND'
    	 *
    	 * @return array of images ID's that are tagged with ALL
    	 * the tags in $taglist.  if $taglist is "cheap, paris, restaurant"
    	 * it will return an array of IDs of images tagged
    	 * cheap AND paris AND restaurant (and not images tagged cheap OR
    	 * paris OR restaurant)
    	 *
    	 * the ID's array can be passed to a find image in db function
    	 * if one need to display a gallery of such images.
    	 *
    	 * otherwise one may use the ID's in nggSinglePicture() function or similar.
    	 *
    	 */
    	function find_images_id_for_tags_and($taglist, $mode = "ASC") {
    		// return the images based on the tag
    		global $wpdb;
    		// extract it into a array
    		$taglist = explode(",", $taglist);
    		if ( !is_array($taglist) )
    			$taglist = array($taglist);
    		$taglist = array_map('trim', $taglist);
    		$new_slugarray = array_map('sanitize_title', $taglist);
    		$sluglist   = "'" . implode("', '", $new_slugarray) . "'";
    
    		$ssql = "
    			SELECT rln.object_id
    			FROM " . $wpdb->term_relationships . " rln
    
    			INNER JOIN " . $wpdb->term_taxonomy . " txn
    			ON rln.term_taxonomy_id = txn.term_taxonomy_id
    
    			INNER JOIN " . $wpdb->terms . " terms
    			ON txn.term_id = terms.term_id
    
    			WHERE terms.slug IN (" . $sluglist . ")
    
    			GROUP BY rln.object_id
    			HAVING COUNT(rln.object_id) > 1
    		";
    
    		$picids = $wpdb->get_col($wpdb->prepare($ssql));
    
    		return $picids;
    
    	}
    
    	/**
    	 * nggTags::find_images_for_tags_and()
    	 *
    	 * @param mixed $taglist
    	 * @param string $mode could be 'ASC' or 'RAND'
    	 *
    	 * @return a gallery based on images' ID obtained with
    	 * find_images_id_for_tags_and()
    	 *
    	 */
    	function find_images_for_tags_and($taglist, $mode = "ASC") {
    
    		$picids = find_images_id_for_tags_and($taglist, $mode);
    
    		//Now lookup in the database
    		if ($mode == 'RAND')
    			$pictures = nggdb::find_images_in_list($picids, true, 'RAND' );
    		else
    			$pictures = nggdb::find_images_in_list($picids, true, 'ASC');			
    
    		return $pictures;
    
    	}

    of course the methods are almost identical to the original find_images_for_tags method. there are not so much comments, but I think the code is sufficiently plain and self-explanatory.

    feel free to (politely 🙂 ) criticize, comment etc.. again, I hope I didn’t reinvent the wheel.

    these hacks are working here:

    http://www.fanta-festival.it/category/anteprime/

    it’s the site of a (mostly horror) movie festival. editors throw in dozens of pictures for each movie (backstage, press releases, maybe posters too…), and in each movie page they wished to relevantly display the movie poster, then some text and then a gallery.

    this way they can tag each image they want in the gallery with the movie name (or better the movie nice-name, the-road and not The Road), and then add also the tag poster to the image they think should be the poster.

    I know that this solution may be an overkill too, and not so efficient also, but I guess its the friendliest for the editors (I don’t know and I don’t want to know how they organized their galleries), and also I really wanted to add this functionality to nextgen… 🙂

    http://wordpress.org/extend/plugins/nextgen-gallery/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter lucaboccianti

    (@lucaboccianti)

    here an example of use:

    // I need to display a single pic of a cheap restaurant in paris
    // (provided, needless to say, that I actually have in some gallery
    // some picture(s) with all those tags)
    
    $pic_id = nggTags::find_images_id_for_tags_and("cheap, restaurant, paris");
    
    // $pic_id is an array of pctures ID, let me have just the first item
    // (and anyway nggSinglePicture need an integer parameter)
    
    $pic_id = $pic_id[0];
    
    // now display that pic
    
    echo nggSinglePicture($pic_id);
    Thread Starter lucaboccianti

    (@lucaboccianti)

    ok, I just realized about a bug. sorry I can’t edit the original message.

    the first method should be:

    /**
    	 * nggTags::find_images_id_for_tags_and()
    	 *
    	 * @param mixed $taglist
    	 * @param string $mode could be 'ASC' or 'RAND'
    	 *
    	 * @return array of images ID's that are tagged with ALL
    	 * the tags in $taglist.  if $taglist is "cheap, paris, restaurant"
    	 * it will return an array of IDs of images tagged
    	 * cheap AND paris AND restaurant (and not images tagged cheap OR
    	 * paris OR restaurant)
    	 *
    	 * the ID's array can be passed to a find image in db function
    	 * if one need to display a gallery of such images.
    	 *
    	 * otherwise one may use the ID's in nggSinglePicture() function or similar.
    	 *
    	 */
    	function find_images_id_for_tags_and($taglist, $mode = "ASC") {
    		// return the images based on the tag
    		global $wpdb;
    		// extract it into a array
    		$taglist = explode(",", $taglist);
    		if ( !is_array($taglist) )
    			$taglist = array($taglist);
    		$tag_count = count($taglist);
    		$taglist = array_map('trim', $taglist);
    		$new_slugarray = array_map('sanitize_title', $taglist);
    		$sluglist   = "'" . implode("', '", $new_slugarray) . "'";
    
    		$ssql = "
    			SELECT rln.object_id
    			FROM " . $wpdb->term_relationships . " rln
    
    			INNER JOIN " . $wpdb->term_taxonomy . " txn
    			ON rln.term_taxonomy_id = txn.term_taxonomy_id
    
    			INNER JOIN " . $wpdb->terms . " terms
    			ON txn.term_id = terms.term_id
    
    			WHERE terms.slug IN (" . $sluglist . ")
    
    			GROUP BY rln.object_id
    			HAVING COUNT(rln.object_id) >= " . $tag_count . "
    		";
    
    		$picids = $wpdb->get_col($wpdb->prepare($ssql));
    
    		return $picids;
    
    	}

    otherwise say you have images tagged “cheap, restaurant, amsterdam” or “expensive, restaurant, paris” they will be listed too.

    Hi,
    I’m sitting in your cheap restaurant in Paris, but your code doesn’t seem to work for me. Although it is exactly the delicatesse my site needs. Can you help me out a bit?

    What i try to solve is this;

    I use the nggsearch plugin. can i use this plugin with your methode? and how do i do that?

    hope to hear from you.

    Arthur

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: NextGEN Gallery] gallery for a tag AND tag(s) hack suggestion’ is closed to new replies.