(这是用于帖子,而不是页面 - 原理相同。永久链接挂钩因具体用例而异)
我刚刚遇到了同样的问题,并创建了一种更方便的方法来做到这一点 - 您不必一直重新编辑您的 functions.php,或者在每次添加时摆弄您的服务器设置(我会两者都不一样)。
TLTR
您可以在您需要的实际 WP 永久链接功能上添加一个过滤器(对我来说它是 post_link,因为我需要存档/类别列表中的该页面别名),并动态地从别名帖子本身。
没关系,因为帖子是别名,所以无论如何您都不需要内容。
第一步打开别名帖子,将引用帖子的ID作为内容
(仅此而已):
接下来,打开你的functions.php并添加:
function prefix_filter_post_permalink($url, $post) {
// if the content of the post to get the permalink for is just a number...
if (is_numeric($post->post_content)) {
// instead, return the permalink for the post that has this ID
return get_the_permalink((int)$post->post_content);
}
return $url;
}
add_filter('post_link', 'prefix_filter_post_permalink', 10, 2 );
就是这样
现在,每次需要创建别名帖子时,只需将引用帖子的ID作为内容,就完成了。
这只会更改永久链接。标题、摘录等将按原样显示,这通常是需要的。此外,PHP 代码中的“它是一个数字”部分远非理想,但为了使这一点易于阅读,您可以进行更多调整。