WordPress中的重写规则应指向index.php, 并将匹配的值(地址中的数字)替换为URL参数(item_id) 您必须使用$matches[1] 而不是$1.
因此,您的规则应该如下所示:
add_rewrite_rule(\'^add-item/([0-9]+)/?\', \'index.php?item_id=$matches[1]\', \'top\');
 通常需要使用有关显示内容的信息来完成规则。用于显示:
单个帖子/页面-添加到重写&p={post_id} 或&postname={post_slug}存档页-&post_type={post_type_slug}自定义类别&{taxonomy_slug}={term_slug}.此规则设置仅适用于item_id 查询var,因此如果您尚未将自己的函数添加到template_include 过滤挂钩,WP将显示主页。
编辑:
Rules with and without index.php重写从开始的规则
index.php 存储在DB中。解析请求时,WordPress从DB中获取它们,并尝试将请求URL与其中一个规则匹配。
不以开头的规则index.php, 不要转到数据库。当WordPress解析请求时,不会检查它们。它们可能会保存在.htaccess 文件but only 如果使用Apache和文件权限,则允许写入。
Why $item_id is NULL
example.com/add-item/1 地址被识别为帖子(帖子类型可以是page 或post) 具有多页(分页后)。你应该知道WordPressremoves page number 来自URLif it equal to "1".example.com/add-item/ 而且它与要求的不一样example.com/add-item/1, 因此将执行重定向。
我不知道这是什么类型的帖子add-item (自定义帖子类型或内置),但请尝试以下方法:
add_action( \'init\', \'se336565_custom_rewrite_basic\' );
add_filter( \'query_vars\', \'se336565_add_custom_query_var\' );
function se336565_add_custom_query_var( $vars ){
  $vars[] = "item_id";
  return $vars;
}
function se336565_custom_rewrite_basic() 
{
    add_rewrite_rule(\'^add-item(?:/([0-9]+))/?$\', 
        \'index.php?post_type={custom_post_type_slug}&pagename=add-item&name=add-item&item_id=$matches[1]\', \'top\');
}
 更换
{custom_post_type_slug} 并刷新重写规则。
最后,在实际页面上使用get_query_var( \'item_id\' ).