WordPress新建页面跳转到指定URL网址

2026-04-17 13:36:46

在Wordpress现有的主题下,新建redirect.php文件模版,在文件中添加如下内容:

<?php/*Template Name: Redirect*/

if (have_posts()) {the_post();$pattern = ‘@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@’;preg_match($pattern, get_the_excerpt(), $matches);if ($matches[0]) header(‘Location: ‘.$matches[0]);else echo ‘Enter a URL into your page body text.’;}?>

保存文件。然后到Wordpress控制台,新建页面,在页面内容中添加你要转到的网址url,最后在编辑页面的右下角,选择页面模板为“Redirect”,保存发布页面就可以了。

猜你喜欢