我在页面中使用以下RewriteRule函数:
function prefix_page_rewrite_rule() {
add_rewrite_rule( '([^/]+)/last', 'index.php?category_name=$matches[1]&last=yes', 'top' );
add_rewrite_rule( '([^/]+)/top', 'index.php?category_name=$matches[1]&top=yes', 'top' );}
add_action( 'init', 'prefix_page_rewrite_rule' );
function prefix_register_query_var( $vars ) {
$vars[] = 'last';
$vars[] = 'top';
return $vars;
}
add_filter( 'query_vars', 'prefix_register_query_var' );
function prefix_url_rewrite_templates() {
if ( get_query_var( 'last' ) ) {
include( get_template_directory() . '/last.php' );
}
if ( get_query_var( 'top' ) ) {
include( get_template_directory() . '/top.php' );
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
它仅适用于具有一个类别(例如url)的页面: sitename.com/category1/top
但是当我想对具有两个类别的页面做同样的事情不起作用时,请使用示例网址: sitename.com/category1/category2/top
我尝试过某事,但它不起作用:
function prefix_page_rewrite_rule() {
add_rewrite_rule( '([^/]+)/last', 'index.php?category_name=$matches[1]&last=yes', 'top' );
add_rewrite_rule( '([^/]+)/top', 'index.php?category_name=$matches[1]&top=yes', 'top' );
add_rewrite_rule( '([^/]+)/([^/]+)/top', 'index.php?category_name=$matches[1]/$matches[2]&top=yes', 'top' );
add_rewrite_rule( '([^/]+)/([^/]+)/last', 'index.php?category_name=$matches[1]/$matches[2]&last=yes', 'top' );}
add_action( 'init', 'prefix_page_rewrite_rule' );
function prefix_register_query_var( $vars ) {
$vars[] = 'last';
$vars[] = 'top';
return $vars;
}
add_filter( 'query_vars', 'prefix_register_query_var' );
function prefix_url_rewrite_templates() {
if ( get_query_var( 'last' ) ) {
include( get_template_directory() . '/last.php' );
}
if ( get_query_var( 'top' ) ) {
include( get_template_directory() . '/top.php' );
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
有人知道解决方案吗?