我正在使用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有一种简单的方法吗?
答案 0 :(得分: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
并从该图库中提取您的图片。