[英]Using URL Rewrite Rules for Dynamic and Pretty URLs in Wordpress
I have a page slugged book-store
. 我的book-store
一页写着。 I want this page to have different sections, namely: category
and book
. 我希望此页面具有不同的部分,即: category
和book
。 I'd like to use rewrite rules to achieve pretty urls that match the following example routes: 我想使用重写规则来实现与以下示例路由匹配的漂亮网址:
example.com/book-store/category/{category_name}
example.com/book-store/book/{book_id}
I've tried the following but I reckon I'm missing something important: 我已经尝试了以下方法,但我认为我缺少了一些重要的东西:
functions.php
: functions.php
:
add_filter('query_vars', 'add_state_var', 0, 1);
function add_state_var($vars)
{
$vars[] = 'category_name';
$vars[] = 'book_id';
return $vars;
}
page-book-store.php
: page-book-store.php
:
add_rewrite_rule('^book-store/category/([^/]*)/?', 'index.php?post_type=page&name=book-store&book-category=$matches[1]', 'top');
add_rewrite_rule('^book-store/book/([^/]*)/?', 'index.php?post_type=page&name=book-store&book-id=$matches[1]', 'top');
I then went into /wp-admin
and clicked save in permalink settings 然后,我进入/wp-admin
并在永久链接设置中单击“保存”
whenever I hit the urls example.com/book-store/category/fiction
or example.com/book-store/book/123
i get a 404 每当我访问网址example.com/book-store/category/fiction
或example.com/book-store/book/123
我都会收到404
I'm using apache2.4 and mysql. 我正在使用apache2.4和mysql。 The mod-rewrite module is enabled in apache. 在Apache中启用了mod-rewrite模块。
What am I missing/doing wrong here? 我在这里想念/做错了什么?
As I understand right: 据我所知:
example.com/book-store/
您有example.com/book-store/
页面 you want to create urls like: 您想要创建以下网址:
example.com/book-store/category/{category_name} example.com/book-store/book/{book_id}
where the {category_name}
and {book_id}
are query vars
其中{category_name}
和{book_id}
是query vars
The code you added into page-book-store.php
file will not work, just because the url was queried before your code. 您添加到page-book-store.php
文件中的代码将不起作用,仅因为在代码之前已查询了url。 Also, the filter add_filter('query_vars', 'add_state_var', 0, 1);
另外,过滤器add_filter('query_vars', 'add_state_var', 0, 1);
will not work, too. 也不会工作。 The priority of this filter is 10, but you used 0. 该过滤器的优先级为10,但您使用的是0。
To achieve your goal add this code into your functions.php
file: 为了实现您的目标,请将以下代码添加到您的functions.php
文件中:
add_action('init', 'ww_rewrite_book_store');
function ww_rewrite_book_store(){
add_rewrite_tag('%book_id%', '([^&]+)');
add_rewrite_rule('^(book-store)/book/([^/]*)/?', 'index.php?pagename=$matches[1]&book_id=$matches[2]', 'top');
add_rewrite_tag('%category_name%', '([^&]+)');
add_rewrite_rule('^(book-store)/category/([^/]*)/?', 'index.php?pagename=$matches[1]&category_name=$matches[2]', 'top');
}
After go to Dashboard > Settings > Permalinks and just click to Save Changes button. 之后,转到“仪表板”>“设置”>“永久链接”,然后单击“ 保存更改”按钮。
First, we used add_rewrite_tag()
to establish the query vars. 首先,我们使用add_rewrite_tag()
建立查询变量。 And used add_rewrite_rule()
to make it work right. 并使用add_rewrite_rule()
使它正常工作。
For example, if your url will be example.com/book-store/category/some_category
, you can use query vars( get {some_category}
) in the page-book-store.php
file like: 例如,如果您的网址为example.com/book-store/category/some_category
,则可以在page-book-store.php
文件中使用查询vars(get {some_category}
),例如:
echo get_query_var('category_name');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.