我是wordpress的新手。
我想使用两个表并显示一些内容,而不创建页面或帖子。
匹配的url将是这样的:
/[category]/[image]
-/nature/green-tree
我的方法是从主题中检查index.php中的url,并拆分url,创建一个仅用于画廊的迷你路线系统。但我认为这不是最聪明的想法。我不想使用图库插件,因为我已经在使用了,这是我需要做的改变。最好的方法是什么?
我想您需要自定义重写规则。以下是你自己做的方法。
如果您使用的是主题,请打开functions.php
文件并输入以下代码,否则,如果您使用插件,请将此代码放在插件中的某个位置,但请确保它立即加载。
function registerCustomUrl($rewrite)
{
$rewrites = $rewrite->rules;
$newRewrites = array();
$newRewrites['([^/]+)/([^/]+)/?$'] = 'index.php?gallery_category=$matches[1]&gallery_image=$matches[2]';
foreach($rewrites as $rk => $rv)
{
$newRewrites[$rk] = $rv;
}
$rewrite->rules = $newRewrites;
}
add_action('generate_rewrite_rules', 'registerCustomUrl');
function registerCustomQueryVars( $vars ) {
$vars[] = 'gallery_category';
$vars[] = 'gallery_image';
return $vars;
}
add_filter('query_vars', 'registerCustomQueryVars');
function myCustomTemplateRedirect()
{
global $wp_query;
if(
isset($wp_query->query_vars['gallery_category']) &&
!empty($wp_query->query_vars['gallery_category']) &&
isset($wp_query->query_vars['gallery_image']) &&
!empty($wp_query->query_vars['gallery_image'])
)
{
// Within your custom template file you are able to
// use any theme function like the get_header(), get_footer(), etc
require_once('path/to/your/custom/template/file.php');
exit(0);
}
}
add_action("template_redirect", 'myCustomTemplateRedirect');