我在理解add\\u rewrite\\u规则在我的案例中是如何工作的方面遇到了困难。我有一个页面列出了来自外部来源的新闻。所有项目均与“链接”;阅读更多信息;。这些链接如下所示:domain.com/news/single/?id=12345&title=title-of-this-news
一切正常。如果我单击链接;“单个”;页面内容为;id“;param并显示我想要的输出。
为了使其美观,我希望链接如下所示:domain.com/news/12345/title-of-this-news
这意味着/单个/不是url的一部分-第一部分是获取其数据所需的ID,最后一部分是标题。
有人能帮我吗?谢谢
EDIT: 由于其中一个答案,我想出了这个来匹配我的URL、参数等,但它不起作用(永久链接被重新保存):
// Register the custom rewrite rule.
function wpse_373628_init() {
add_rewrite_rule(
// Matches /news/<id>/<title-slug> in the URL.
\'^news-events/single/(\\d+)/([^/]+)/?$\',
// You could also add &news_title=$matches[2], but I think the ID is
// enough for you to identify the single news being requested.
\'index.php?pagename=news-events/single&newsID=$matches[1]&newsTitle=$matches[2]\', // query by slug
// \'index.php?page_id=123&news_id=$matches[1]\', // or query by ID
\'top\'
);
}
add_action( \'init\', \'wpse_373628_init\' );
// And register the custom query arg.
function wpse_373628_query_vars( $vars ) {
$vars[] = \'newsID\';
$vars[] = \'newsTitle\'; // if you\'re using news_title in the rewrite rule
return $vars;
}
add_filter( \'query_vars\', \'wpse_373628_query_vars\' );