0

我正在使用 NextGEN Gallery 插件。目前我正在通过mysite.com/photography?cat=weddings获取类别 ID

在我的 page.php 中,我获得了 url 中的类别并根据该类别填充页面。

$cat = $_GET['cat'];
if($cat == 'portraits'){ $cat = 'where galleryid = 5'; }
if($cat == 'weddings'){ $cat = 'where galleryid = 3'; }
if($cat == 'outdoor'){ $cat = 'where galleryid = 4'; }

$q = $wpdb->get_results("
   select filename, meta_data, galleryid, gal.name, pid
   from wp_ngg_pictures as pic
   join wp_ngg_gallery as gal on (gal.gid = pic.galleryid)
   {$cat}
");

我只想这样做/mysite.com/photography/weddings 并且 URL 中没有查询字符串。我也不想做所有这些 if 语句来获取 ID。我根本不希望 ID 出现在 URL 中,所以我想要一个可读性好的 url。

wordpress 有一个简单的方法吗?

4

1 回答 1

1

将此添加到您的functions.php以添加新的重写规则:

function my_add_rewrites() {
    global $wp_rewrite;
    $wp_rewrite->add_rule('photography/([^/]+)/?', 'index.php?pagename=photography&photocat=$matches[1]', 'top');
}
add_action( 'init', 'my_add_rewrites' );

function my_query_vars( $query_vars ) {
    $query_vars[] = 'photocat';
    return $query_vars;
}
add_filter( 'query_vars', 'my_query_vars' );

然后在你的page.php,添加:

global $wp, $wpdb;
// Get the query var
$photocat = $wp->query_vars['photocat'];
// Get the gallery ID
$gid = $wpdb->get_var( "SELECT gid FROM $wpdb->ngg_gallery WHERE slug = '$photocat';" );
// Check if we have a valid value
$q = FALSE; // Set a default so you can check later
if( $gid !== NULL ) {
    // Get the images -> could also use $nggdb->get_gallery($gid);
    $q = $wpdb->get_results("
        select filename, meta_data, galleryid, gal.name, pid
        from wp_ngg_pictures as pic
        join wp_ngg_gallery as gal on (gal.gid = pic.galleryid)
        {$gid}
    ");
}

您还应该能够从$nggdb->get_gallery($gid)可能更容易和更清洁的图库中获取图像。

添加所有(重写)代码后,转到Settings -> Permalinks并重新保存您的永久链接设置以将新的重写规则添加到数据库。

mysite.com/photography/weddings现在应该将$photocat变量设置为weddings并从该图库中提取图像。

于 2013-10-12T06:09:21.760 回答