Using nextGen Gallery.. how can I call a list of all Gallery Names?

I’m trying to make a nav to access all the different galleries on a single page.

Instead of hard-coding all their names out. I was curious if it was possible to make this dynamic and just call all the ID’s of all the galleries, with maybe an exclusionary on the two that are slideshows.

Read More

Anyone got any fresh ideas?

Related posts

Leave a Reply

3 comments

  1. All your Galleries are stored a table called wp_ngg_gallery in your wordpress database. (For the sake of completeness: nextGen Gallery creates 2 more tables for albums and pictures, respectively)

    $gallery_ids = $wpdb->get_results(
        "SELECT gid FROM ".$wpdb->prefix."ngg_gallery ORDER BY gid ASC", ARRAY_A);
    

    fetches the IDs of all galleries into an array, ordered by ID from 1 to end. Now for excluding the two that are slideshows (the example will assume their IDs are 4 and 25):

    $gallery_ids = $wpdb->get_results(
        "SELECT gid
         FROM ".$wpdb->prefix."ngg_gallery
         WHERE gid NOT IN (4, 25)
         ORDER BY gid ASC", ARRAY_A);
    

    What good are the IDs without the galleries’ titles? Also, wouldn’t it be easier to exclude by title? So this

    $galleries = $wpdb->get_results(
        "SELECT gid, title
         FROM ".$wpdb->prefix."ngg_gallery
         WHERE title NOT IN (slideshow_1, slideshow_2)
         ORDER BY title ASC", ARRAY_A);
    

    would give you an array of all non-slideshow galleries ordered alphabetically by gallery title. You could also select with a wildcard * instead of gid, title, then you’d get all columns from the gallery table, which are gid, name, slug, path, title, galdesc, pageid, previewpic, author.

    The resulting array is obviously meaningless unless you do something with it, such as iterating over it and creating a list of links.

    The structure of the resulting array is:

    Array
    (
        [0] => Array
            (
                ['gid'] => 4
                ['title'] => 'playing_football'
            )
        [1] => Array
            (
                ['gid'] => 8
                ['title'] => 'vacation_pics'
            )
    )
    

    You get the idea.

    EDIT: Creating a navigation from the above database query

    Assuming that you already have created pages for all galleries from within the NextGen Gallery > Manage Gallery dialog, the following will create a simple navigation from it. The pageid and title columns must have been selected from the databse.

    echo '<ul>';
    
    foreach ( $galleries as $gallery ) {
        echo '<li><a href="' .
             get_bloginfo( 'url' ) .
             '/?p=' .
             $gallery['pageid'] .
             '">' .
             $gallery['title'] .
             '</a></li>';
    }
    
    echo '</ul>';
    

    Reading, in case you care:

  2. If anyone needs to add a menu of galleries using a certain album on a certain page can use my code.

    <div id="gallery_menu">
            <?php
    
                // hardcoded part
                if(is_page(12)){
                    $album_id = 2;
                    $page_id = 12;
                }elseif(is_page(14)){
                    $album_id = 3;
                    $page_id = 14;
                }elseif(is_page(16)){
                    $album_id = 4;
                    $page_id = 16;
                }else{
                    $album_id = 2;
                    $page_id = 12;
                }
    
                // get the needed albums gallery id's
                $query = "SELECT sortorder FROM wp_ngg_album WHERE id=".(int)$album_id;
                $result = mysql_query($query) or die(mysql_error());
                while($row = mysql_fetch_array($result)){
                    if($row['sortorder']){
                        $tmp = unserialize($row['sortorder']);
                    }
                }
    
                // get gallery titles according to id's we got
                $query_titles = "SELECT title, gid FROM wp_ngg_gallery WHERE gid IN(";
                for($i=0; $i < count($tmp); $i++){
                    if($i != (count($tmp)-1)){
                        $query_titles .= (int)$tmp[$i].", ";
                    }elseif($i == (count($tmp)-1)){
                        $query_titles .= (int)$tmp[$i].")";
                    }
                }
    
                // query and show titles with links
                $result_titles = mysql_query($query_titles) or die(mysql_error());
                echo '<ul class="gallery_titles">';
                    while($row_titles = mysql_fetch_array($result_titles)){
                        echo '<li><a href="'.get_bloginfo('siteurl').'/?p='.$page_id.'&album='.$album_id.'&gallery='.$row_titles['gid'].'">'.$row_titles['title'].'</a></li>';
                    }
                echo '</ul>';
    
            ?>
        </div>