php 创建重定向到另一个 URL 的 WordPress 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1698797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:33:12  来源:igfitidea点击:

Create WordPress Page that redirects to another URL

phpwordpress

提问by Dscoduc

I wanted to create a new WordPress page that is actually a link to another site. The goal is to have the page show up in a list of my pages, but actually send the web user to the target URL.

我想创建一个新的 WordPress 页面,它实际上是指向另一个站点的链接。目标是让页面显示在我的页面列表中,但实际上将网络用户发送到目标 URL。

For example, say I want to include a page that indicates "My Photos" but actually redirects them to Flickr.

例如,假设我想要包含一个显示“我的照片”但实际上将它们重定向到 Flickr 的页面。

I'm guessing one way to accomplish this is by using a custom template page with a redirect instruction in PHP, but unfortunately I am a newbie to PHP and am not familiar with the way to accomplish this...

我猜想实现这一点的一种方法是在 PHP 中使用带有重定向指令的自定义模板页面,但不幸的是,我是 PHP 的新手,并且不熟悉实现此目的的方法......

回答by Schoffelman

You can accomplish this two ways, both of which need to be done through editing your template files.

您可以通过两种方式完成这两种方式,这两种方式都需要通过编辑您的模板文件来完成。

The first one is just to add an html link to your navigation where ever you want it to show up.

第一个是在导航中添加一个 html 链接到您希望它显示的位置。

The second (and my guess, the one you're looking for) is to create a new page template, which isn't too difficult if you have the ability to create a new .php file in your theme/template directory. Something like the below code should do:

第二个(我猜也是你正在寻找的)是创建一个新的页面模板,如果你有能力在你的主题/模板目录中创建一个新的 .php 文件,这并不是太困难。像下面的代码应该做的事情:

<?php /*  
Template Name: Page Redirect
*/ 

header('Location: http://www.nameofnewsite.com');
exit();

?>

Where the template name is whatever you want to set it too and the url in the header function is the new url you want to direct a user to. After you modify the above code to meet your needs, save it in a php file in your active theme folder to the template name. So, if you leave the name of your template "Page Redirect" name the php file page-redirect.php.

其中模板名称也是您想要设置的任何内容,标题函数中的 url 是您希望将用户定向到的新 url。修改上面的代码以满足您的需要后,将其保存在您活动主题文件夹中的php文件中,以模板名称命名。因此,如果您将模板的名称保留为“页面重定向”,请将 php 文件命名为 page-redirect.php。

After that's been saved, log into your WordPress backend, and create a new page. You can add a title and content to the body if you'd like, but the important thing to note is that on the right hand side, there should be a drop down option for you to choose which page template to use, with default showing first. In that drop down list, there should be the name of the new template file to use. Select the new template, publish the page, and you should be golden.

保存后,登录到您的 WordPress 后端,并创建一个新页面。如果您愿意,您可以向正文添加标题和内容,但需要注意的是,在右侧,应该有一个下拉选项供您选择要使用的页面模板,默认显示第一的。在该下拉列表中,应该有要使用的新模板文件的名称。选择新模板,发布页面,你应该是金子了。

Also, you can do this dynamically as well by using the Custom Fields section below the body editor. If you're interested, let me know and I can paste the code for that guy in a new response.

此外,您也可以使用正文编辑器下方的自定义字段部分动态执行此操作。如果您有兴趣,请告诉我,我可以将那个人的代码粘贴到新的回复中。

回答by Jason Leveille

I've found that these problems are often best solved at the server layer. Do you have access to an .htaccess file where you could place a redirect rule? If so:

我发现这些问题通常最好在服务器层解决。您是否有权访问可以放置重定向规则的 .htaccess 文件?如果是这样的话:

RedirectPermanent /path/to/page http://uri.com

This redirect will also serve a "301 Moved Permanently" response to indicate that the Flickr page (for example) is the permanent URI for the old page.

此重定向还将提供“301 Moved Permanently”响应,以指示 Flickr 页面(例如)是旧页面的永久 URI。

If this is not possible, you can create a custom page template for each page in question, and add the following PHP code to the top of the page template (actually, this is all you need in the template:

如果这不可行,您可以为每个有问题的页面创建一个自定义页面模板,并将以下 PHP 代码添加到页面模板的顶部(实际上,这就是您在模板中需要的全部内容:

header('Location: http://uri.com, true, 301');

More information about PHP headers.

有关 PHP 标头的更多信息

回答by Gipetto

Alternately, use a filter.

或者,使用过滤器。

Create an empty page in your WordPress blog, named appropriately to what you need it to be. Take note of the post_id. Then create a filter that alters its permalink.

在您的 WordPress 博客中创建一个空白页面,并根据您的需要适当命名。记下 post_id。然后创建一个改变其固定链接的过滤器。

add_filter('get_the_permalink','my_permalink_redirect');
function my_permalink_redirect($permalink) {
    global $post;
    if ($post->ID == your_post_id_here) {
        $permalink = 'http://new-url.com/pagename';
    }
    return $permalink;
}

This way the url will show up correctly in the page no funny redirects are required.

这样 url 将正确显示在页面中,不需要有趣的重定向。

If you need to do this a lot, then think about using the custom postmeta fields to define a postmeta value for "offsite_url" or something like that, then you can create pages as needed, enter the "offsite_url" value and then use a filter like the one above to instead of checking the post_id you check to see if it has the postmeta required and alter the permalink as needed.

如果你需要这样做很多,那么考虑使用自定义 postmeta 字段为“offsite_url”或类似的东西定义 postmeta 值,然后你可以根据需要创建页面,输入“offsite_url”值,然后使用过滤器像上面那样,而不是检查 post_id,而是检查它是否具有所需的 postmeta 并根据需要更改永久链接。

回答by BraedenP

I'm not familiar with Wordpress templates, but I'm assuming that headers are sent to the browser by WP before your template is even loaded. Because of that, the common redirection method of:

我不熟悉 Wordpress 模板,但我假设在您的模板加载之前,WP 将标题发送到浏览器。因此,常见的重定向方法为:

header("Location: new_url");

won't work. Unless there's a way to force sending headers through a template before WP does anything, you'll need to use some Javascript like so:

不会工作。除非有一种方法可以在 WP 执行任何操作之前强制通过模板发送标头,否则您需要使用一些 Javascript,如下所示:

<script language="javascript" type="text/javascript">
document.location = "new_url";
</script>

Put that in the section and it'll be run when the page loads. This method won't be instant, and it also won't work for people with Javascript disabled.

将其放在该部分中,它将在页面加载时运行。这种方法不是即时的,它也不适用于禁用 Javascript 的人。

回答by Jeroenv3

Use the "raw" plugin https://wordpress.org/plugins/raw-html/Then it's as simple as:

使用“raw”插件https://wordpress.org/plugins/raw-html/然后就这么简单:

[raw]
<script>
window.location = "http://www.site.com/new_location";
</script>
[/raw]

回答by Ryan

I found a plugin that helped me do this within seconds without editing code:

我找到了一个插件,可以帮助我在几秒钟内完成此操作,而无需编辑代码:

https://wordpress.org/plugins/quick-pagepost-redirect-plugin/

https://wordpress.org/plugins/quick-pagepost-redirect-plugin/

I found it here: http://premium.wpmudev.org/blog/wordpress-link-title-external-url/

我在这里找到它:http: //premium.wpmudev.org/blog/wordpress-link-title-external-url/

回答by Mazhar

There are 3 ways of doing this:

有3种方法可以做到这一点:

  1. By changing your 404.phpcode.
  2. By using wordpress plugins.
  3. By editing your .htaccessfile.
  1. 通过更改您的404.php代码。
  2. 通过使用 wordpress 插件。
  3. 通过编辑您的.htaccess文件。

Complete tutorial given at http://bornvirtual.com/wordpress/redirect-404-error-in-wordpress/906/

完整教程在http://bornvirtual.com/wordpress/redirect-404-error-in-wordpress/906/

回答by Michael.M

There is a much simpler way in wordpress to create a redirection by using wordpress plugins. So here i found a better way through the plugin Redirectionand also you can find other as well on this site Create Url redirect in wordpress through Plugin

在 wordpress 中有一种更简单的方法来使用 wordpress 插件创建重定向。所以在这里我通过插件重定向找到了一个更好的方法,你也可以在这个站点上找到其他的通过插件在 wordpress 中创建 URL 重定向