Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Plugins & Atahualpa (http://forum.bytesforall.com/forumdisplay.php?f=16)
-   -   Adding Search to NextGEN-Gallery (http://forum.bytesforall.com/showthread.php?t=1484)

drewpasmith Apr 30, 2009 12:56 AM

Adding Search to NextGEN-Gallery
 
Hi All,

I've found the following instructions to activate searching of tagged images in NextGEN-Gallery but I can't find the PHP file they refer to:

1) Add this code in your file search.php (inside your template folder), right after <h2 class="pagetitle">Search Results</h2> or somewhere (depending your template) after <?php if (have_posts()) : ?>, otherwise it wouldn’t find the pictures.

Pay a special attention to the line $nggpictures = ngg_get_search_pictures($keywords, '');
since on the second function argument ( ” ) you may indicate the number of pictures you want to display on each row result. IE, if you want to get 6 pictures displayed on each row, you have to change the line to $nggpictures = ngg_get_search_pictures($keywords, '6');

By default, 4 pictures for each row are displayed. Note that you may also split the code, and put a part before and another part after the <?php while [...] ?> function with the result to display pictures (if relevent) even if there is no post. That’s a bit more advanced, but people with advanced php skills won’t need any complementary explanation to achieve that.

2) Add this code (or this code if you use NexGen 1.0) anywhere in your file functions.php (inside your template folder, create one if you don’t have any)


The code for step on is

Code:

if(is_search()) {
        $search = $wp_query->get('s');
        $keywords = preg_replace('/\+/',' ',$search);
        if (function_exists ('ngg_get_search_pictures')) {  // function from functions.php
                $nggpictures = ngg_get_search_pictures($keywords, ''); // put the number of pictures by row you want, if you don't want "4"

                if ($nggpictures) {
                        echo "<h2>Pictures</h2>";
                        echo $nggpictures;       
                }
        }
}


The code for step 2 is:

Code:

## Function to do searchs on gallery pics from NextGen Gallery plugin
##
## 2 vars :        (1) $keywords (usually coming from the standard search query from wordpress)
##                (2) $numberPicCol (number of pic by row, if null it takes 4 )
function ngg_get_search_pictures ($keywords, $numberPicRow = NULL) {
        global $wpdb;
        $count=1;
        if (!$numberPicRow) { $numberPicRow = "4"; }

        $nngquery = "
                                SELECT pid,description,alttext
                                FROM wp_ngg_pictures
                                WHERE MATCH (description, filename, alttext) AGAINST ('*$keywords*' IN BOOLEAN MODE)
                                AND exclude = '0'
                                ";
        $pictures = $wpdb->get_results($nngquery, ARRAY_A);

        if ($pictures) foreach($pictures as $pic) {
                               
                $out .= '<a href="'.nggallery::get_image_url($pic[pid]).'" title="'.stripslashes($pic[description]).'" class="thickbox" rel="singlepic'.$pic[pid].'">';
                $out .=  '<img class="ngg-singlepic" src="'.nggallery::get_thumbnail_url($pic[pid]).'" alt="'.stripslashes($pic[alttext]).'" title="'.stripslashes($pic[alttext]).'" />';
                $out .=  "</a>\n";
                                if ($count == 0) {
                                $out .= "<br />";
                                }
                                ++$count;
                                $count%=$numberPicRow;
        }
        return $out;
};

Anybody know where I can find the search.php equivalent or the <?php if (have_posts()) : ?> to insert the new code after?

Cheers,

Drew

drewpasmith May 13, 2009 04:07 AM

*Bump* Anyone?

Flynn May 13, 2009 06:57 PM

Make a copy of index.php as search.php and put the code there.

Wrap both pieces of code into opening and closing php tags
<?php ... ?>

For the 2nd code, for functions.php, make sure you don't create blank lines anywhere outside of opening and closing PHP tags, anywhere in functions.php

digimarc Jun 9, 2009 12:15 PM

I have been fighting with this for days now and not getting any image results back. I think I found a fix. I have adjusted it below. The important part is the $wpdb->prefix statement. Combined with the information from Flynn, I got everything working, finally!

Code:

FROM " .$wpdb->prefix. "ngg_pictures
Code:

/**
** Function to do searchs on gallery pics from NextGen Gallery plugin
**
** @keywords                keywords, usually gave by the standard search query from wordpress
** @numberPicCol        number of pic by row. If null, it takes 4 by default
*/
function ngg_get_search_pictures ($keywords, $numberPicRow = NULL) {
        global $wpdb;
        $count=1;
        if (!$numberPicRow) { $numberPicRow = "4"; }

        $nngquery = $wpdb->prepare( "
                                SELECT pid,description,alttext
                                FROM " .$wpdb->prefix. "ngg_pictures
                                WHERE MATCH (description, filename, alttext) AGAINST (%s IN BOOLEAN MODE)
                                AND exclude != 1"
                                , "*".$keywords."*");
        $pictures = $wpdb->get_results($nngquery, ARRAY_A);

        if ($pictures) foreach($pictures as $pic) {
                               
                $out .= '<a href="'.nggGallery::get_image_url($pic["pid"]).'" title="'.stripslashes($pic[description]).'" class="thickbox" rel="singlepic'.$pic[pid].'">';
                $out .=  '<img class="ngg-singlepic" src="'.nggGallery::get_thumbnail_url($pic[pid]).'" alt="'.stripslashes($pic[alttext]).'" title="'.stripslashes($pic[alttext]).'" />';
                $out .=  "</a>\n";
               
                if ($count == 0) {
                        $out .= "<br />";
                }
               
                ++$count;
                $count%=$numberPicRow;
        }
        return $out;
};


thirdpotato Aug 24, 2009 07:06 PM

Hey I just got a syntax error when I entered your new edited code below. What am I doing wrong? I added the stuff you said, in the search.php then in functions.php I just copy and pasted the last box of coding you've posted. So what do I do?

flieger28 Sep 28, 2009 10:28 AM

Maybe someone could post a sample search.php ?
And i don't understand how to put the search.php on the Menu on my page.
Or should I make a New Page in Wordpress, called Search and copy the content of the index.php and the code in it?

allforoneinsultz Feb 3, 2010 12:56 PM

[quote=Flynn;6611]Make a copy of index.php as search.php and put the code there.
Wrap both pieces of code into opening and closing php tags
<?php ... ?>

Soooooooooo That would be like this?.............?
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/

/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
?><?php
if(is_search()) {
$search = $wp_query->get('s');
$keywords = preg_replace('/\+/',' ',$search);
if (function_exists ('ngg_get_search_pictures')) { // function from functions.php
$nggpictures = ngg_get_search_pictures($keywords, ''); // put the number of pictures by row you want, if you don't want "4"

if ($nggpictures) {
echo "<h2>Pictures</h2>";
echo $nggpictures;
}
}
}
php?>

For the 2nd code, for functions.php, make sure you don't create blank lines anywhere outside of opening and closing PHP tags, anywhere in functions.php

Where Exactly should I put this in the code?[/QUOTE]



[quote=digimarc;8160]I have been fighting with this for days now and not getting any image results back. I think I found a fix. I have adjusted it below. The important part is the $wpdb->prefix statement. Combined with the information from Flynn, I got everything working, finally!
QUOTE]

If you understand my problem written above here.. then please explain me how you did manage to get it working.


Thanks,

Cerafil

Davidson Feb 14, 2010 06:35 AM

Will you be adding a search capability to the new update. ... like the search fonction of wordpress search into NextGen Gallery pictures too.............

mutpowr Jun 15, 2011 10:16 AM

Sorry for the necro post but im working on adding this as well. Does anyone know where he meant the

Code:

FROM " .$wpdb->prefix. "ngg_pictures
code to go?

mutpowr Jun 15, 2011 10:29 AM

So i got it working just so everyone knows and i wanted to add some small tid bits to help others having this problem.

1) Go to the atahualpa theme folder and copy index.php and rename it to search.php

2) In search.php

Find:

Code:

<?php /* If there are any posts: */
Before add:

Code:

<?php if(is_search()) {
        $search = $wp_query->get('s');
        $keywords = preg_replace('/\+/',' ',$search);
        if (function_exists ('ngg_get_search_pictures')) {  // function from functions.php
                $nggpictures = ngg_get_search_pictures($keywords, ''); // put the number of pictures by row you want, if you don't want "4"

                if ($nggpictures) {
                        echo "<h2>Pictures</h2>";
                        echo $nggpictures;       
                }
        }
} ?>

3) Find functions.php in the atahualpa theme folder

4) In Functions.php

Find:

Code:

<?php
Before Add:

Code:

<?php /**
** Function to do searchs on gallery pics from NextGen Gallery plugin
**
** @keywords                keywords, usually gave by the standard search query from wordpress
** @numberPicCol        number of pic by row. If null, it takes 4 by default
*/
function ngg_get_search_pictures ($keywords, $numberPicRow = NULL) {
        global $wpdb;
        $count=1;
        if (!$numberPicRow) { $numberPicRow = "4"; }

        $nngquery = $wpdb->prepare( "
                                SELECT pid,description,alttext
                                FROM " .$wpdb->prefix. "ngg_pictures
                                WHERE MATCH (description, filename, alttext) AGAINST (%s IN BOOLEAN MODE)
                                AND exclude != 1"
                                , "*".$keywords."*");
        $pictures = $wpdb->get_results($nngquery, ARRAY_A);

        if ($pictures) foreach($pictures as $pic) {
                               
                $out .= '<a href="'.nggGallery::get_image_url($pic["pid"]).'" title="'.stripslashes($pic[description]).'" class="thickbox" rel="singlepic'.$pic[pid].'">';
                $out .=  '<img class="ngg-singlepic" src="'.nggGallery::get_thumbnail_url($pic[pid]).'" alt="'.stripslashes($pic[alttext]).'" title="'.stripslashes($pic[alttext]).'" />';
                $out .=  "</a>\n";
               
                if ($count == 0) {
                        $out .= "<br />";
                }
               
                ++$count;
                $count%=$numberPicRow;
        }
        return $out;
};?>

Note - The only problem i can find is when you search you may get the error that the item hasnt been found. However the images will still show relating to the search. So not sure on how to fix that one just yet but here is the fix im sure people would want.

lmilesw Jun 17, 2011 06:48 AM

The latest version of NextGen has support for XML sitemaps if you use Yoast's SEO plugin.


All times are GMT -6. The time now is 11:03 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.