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页面,它实际上是另一个站点的链接。目的是让页面显示在我的页面列表中,但实际上是将web用户发送到目标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新手,不熟悉实现这一点的方法……
7 个解决方案
#1
33
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.
模板名是你想要设置的,header函数中的url是你想要引导用户的新url。在您修改上面的代码以满足您的需求之后,将其保存到活动主题文件夹中的php文件中,并将其保存到模板名称中。因此,如果您留下模板的名称“页面重定向”,请命名php文件页面-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.
此外,您还可以通过使用body编辑器下面的Custom Fields一节来动态地实现这一点。如果您感兴趣,请告诉我,我可以在新的响应中粘贴这个家伙的代码。
#2
11
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永久移动”响应,以指示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头的更多信息。
#3
7
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。然后创建一个改变其permalink的过滤器。
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字段定义一个postmeta值“offsite_url”或类似的东西,然后你可以根据需要创建页面,输入“offsite_url”值,然后使用一个过滤器像上面,而不是检查post_id你检查是否有postmeta要求并根据需要改变永久链接。
#4
3
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.
把它放到section中,当页面加载时就会运行。这个方法不会是即时的,它也不会对禁用Javascript的人起作用。
#5
1
Use the "raw" plugin https://wordpress.org/plugins/raw-html/ Then it's as simple as:
使用“原始”插件https://wordpress.org/plugins/raw-html/,它就简单到:
[raw]
<script>
window.location = "http://www.site.com/new_location";
</script>
[/raw]
#6
0
There are 3 ways of doing this:
有三种方法可以做到这一点:
- By changing your
404.php
code. - 通过改变你的404。php代码。
- By using wordpress plugins.
- 通过使用wordpress插件。
- By editing your
.htaccess
file. - 通过编辑.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/给出
#7
0
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- titleexternalurl/
#1
33
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.
模板名是你想要设置的,header函数中的url是你想要引导用户的新url。在您修改上面的代码以满足您的需求之后,将其保存到活动主题文件夹中的php文件中,并将其保存到模板名称中。因此,如果您留下模板的名称“页面重定向”,请命名php文件页面-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.
此外,您还可以通过使用body编辑器下面的Custom Fields一节来动态地实现这一点。如果您感兴趣,请告诉我,我可以在新的响应中粘贴这个家伙的代码。
#2
11
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永久移动”响应,以指示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头的更多信息。
#3
7
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。然后创建一个改变其permalink的过滤器。
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字段定义一个postmeta值“offsite_url”或类似的东西,然后你可以根据需要创建页面,输入“offsite_url”值,然后使用一个过滤器像上面,而不是检查post_id你检查是否有postmeta要求并根据需要改变永久链接。
#4
3
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.
把它放到section中,当页面加载时就会运行。这个方法不会是即时的,它也不会对禁用Javascript的人起作用。
#5
1
Use the "raw" plugin https://wordpress.org/plugins/raw-html/ Then it's as simple as:
使用“原始”插件https://wordpress.org/plugins/raw-html/,它就简单到:
[raw]
<script>
window.location = "http://www.site.com/new_location";
</script>
[/raw]
#6
0
There are 3 ways of doing this:
有三种方法可以做到这一点:
- By changing your
404.php
code. - 通过改变你的404。php代码。
- By using wordpress plugins.
- 通过使用wordpress插件。
- By editing your
.htaccess
file. - 通过编辑.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/给出
#7
0
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- titleexternalurl/