第三个参数告诉WordPress要使用/匹配的查询变量。
我们可以通过以下请求使用这些公共查询变量查询WordPress数据库example.com/?foo1=bar1&foo2=bar2 但我们通常想把它改写成更漂亮的东西,比如example.com/bar1/bar2/.
在法典中there\'s a list 默认可用的公共查询变量:
attachment
attachment_id
author
author_name
cat
category_name
comments_popup
day
error
feed
hour
m
minute
monthnum
name
p
page_id
paged
pagename
post_parent
post_type
preview
second
static
subpost
subpost_id
tag
tag_id
tb
w
year
示例1:如果我们创建一个自定义分类法,例如
country, 然后,我们将自动获得:
add_rewrite_tag( \'%country%\', \'([^/]+)\', \'country=\' );
其中,第三个参数是必须以结尾的查询变量
=.
示例2:
如果要使用permalink结构
example.com/article-1984 对于
post 仅post类型,然后我们可以引入自定义重写标记
%article-pid%:
add_action( \'init\', function(){
add_rewrite_tag( \'%article-pid%\', \'article-([0-9]+)\', \'p=\' );
});
请注意,这里的查询变量是
p 它必须以
=.
请求example.com/article-1984 因此被解释为example.com/?p=1984, 刷新重写规则后,例如通过重新保存永久链接设置。
然后我们必须修改get_permalink() 相应的功能:
add_filter( \'post_link\', function( $post_link, $post ) {
return str_replace( \'%article-pid%\', \'article-\' . $post->ID, $post_link );
}, 10, 2 );
在这种情况下,永久链接设置是这样的:

如果我们使用article-%post_id% 相反,它似乎打乱了其他自定义帖子类型和分类法的重写规则。