好的,我添加了一个定制的永久链接,它可以很好地映射到我的页面。对于某些区域,虽然我添加的过滤器没有显示出来。在短代码中,它们只是作为NULL返回。
我的代码是;
function custom_rewrite( $wp_rewrite ) {
$feed_rules = array(
\'local/(.*?)/(.*?)/?$\' => \'index.php?page_id=41&trade=\' . $wp_rewrite->preg_index(1).\'&area=\' . $wp_rewrite->preg_index(2),
\'local/(.*?)/(.*?)?$\' => \'index.php?page_id=41&trade=\' . $wp_rewrite->preg_index(1).\'&area=\' . $wp_rewrite->preg_index(2),
);
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
function custom_wp_querystring() {
add_rewrite_tag(\'%trade%\',\'([^&]+)\');
add_rewrite_tag(\'%area%\',\'([^&]+)\');
}
add_action( \'init\', \'custom_wp_querystring\');
add_filter( \'generate_rewrite_rules\', \'custom_rewrite\' );
add_shortcode(\'rewrite_local\', \'rewriteShortcode\');
function rewriteShortcode() {
echo "vars are";
var_dump($wp_query->query_vars[\'trade\']);
var_dump($wp_query->query_vars[\'area\']);
}
是不是我错过了一些简单的事情?
SO网友:JasonC
也许在custom_rewrite
函数,尝试连接$wp_rewrite->rules
和$feed_rules
使用array_merge
, 然后返回$wp_rewrite
.
function custom_rewrite( $wp_rewrite ) {
$feed_rules = array(
\'local/(.*?)/(.*?)/?$\' => \'index.php?page_id=41&trade=\' . $wp_rewrite->preg_index(1).\'&area=\' . $wp_rewrite->preg_index(2),
\'local/(.*?)/(.*?)?$\' => \'index.php?page_id=41&trade=\' . $wp_rewrite->preg_index(1).\'&area=\' . $wp_rewrite->preg_index(2),
);
$wp_rewrite->rules = array_merge($feed_rules, $wp_rewrite->rules);
return $wp_rewrite;
}
我根本没有测试过,只是一个建议。