我创建了一个自定义页面模板movie-page.php
和一页movie
在WP中。当我访问时,模板显示良好site.com/movie/
我想添加movie_id
作为查询变量。以下是我的代码:
add_filter(\'query_vars\', \'poe_query_vars\');
function poe_query_vars( $vars ) {
$vars[] = \'movie_id\';
return $vars;
}
我想添加重写规则,以便
site.com/movie/123/ 将给我movie\\u id=123。我不关心页码,尽管我可以使用
page
查询var,我需要学习如何正确地执行此操作。
add_filter(\'rewrite_rules_array\', \'poe_rewrite_rules\');
function poe_rewrite_rules($rules) {
$custom = array(\'movie/([0-9]+)/?$\' => \'index.php?pagename=movie&movie_id=$matches[1]\');
return $custom + $rules;
}
我已经单击了保存永久链接,可以验证规则是否存在。该规则位于规则的顶部,下面是由自定义页面模板创建的其他规则。在我的电影页面中。php模板,我正在响应重写规则,查询变量和
/movie/123/ 给我一部电影\\u id=>123。
Edit - 添加与电影相关的重写规则以供参考
\'/movie/([0-9]+)/([0-9]+)/?$\' => \'index.php?pagename=movie&movie_id=$1&page=$2\',
\'/movie/([0-9]+)/?$\' => \'index.php?pagename=movie&movie_id=$1\',
...
\'movie/attachment/([^/]+)/?$\' => \'index.php?attachment=$1\',
\'movie/attachment/([^/]+)/trackback/?$\' => \'index.php?attachment=$1&tb=1\',
\'movie/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?attachment=$1&feed=$2\',
\'movie/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?attachment=$1&feed=$2\',
\'movie/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\' => \'index.php?attachment=$1&cpage=$2\',
\'(movie)/trackback/?$\' => \'index.php?pagename=$1&tb=1\',
\'(movie)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?pagename=$1&feed=$2\',
\'(movie)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?pagename=$1&feed=$2\',
\'(movie)/page/?([0-9]{1,})/?$\' => \'index.php?pagename=$1&paged=$2\',
\'(movie)/comment-page-([0-9]{1,})/?$\' => \'index.php?pagename=$1&cpage=$2\',
\'(movie)(/[0-9]+)?/?$\' => \'index.php?pagename=$1&page=$2\',
...