wordpress链接跳转
Do you want to add an external link as post title in WordPress? Sometimes you may just want to share a link with your users. Instead of sending them to a post, you may want the post title to link to the other website. In this article, we will show you how to link to external links from the post title in WordPress.
您是否要在WordPress中添加外部链接作为帖子标题? 有时,您可能只想与用户共享链接。 您可能希望将帖子标题链接到其他网站,而不是将其发送到帖子。 在本文中,我们将向您展示如何从WordPress中的帖子标题链接到外部链接。
This method is easier and is recommended for beginners.
此方法更简单,建议初学者使用。
First thing you need to do is install and activate the Page Links To plugin. For more details, see our step by step guide on how to install a WordPress plugin.
您需要做的第一件事是安装并激活“ 页面链接到”插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。
Upon activation, simply create a new post or edit an existing one. You will notice the new ‘Page Links To’ meta box below the post editor.
激活后,只需创建一个新帖子或编辑现有帖子即可。 您会注意到帖子编辑器下方的新“页面链接至”元框。
Click on ‘A custom URL’ to add the link you want to add to post title. Now you can save or publish your post.
单击“自定义URL”以添加要添加到帖子标题的链接。 现在,您可以保存或发布您的帖子。
That’s all. The post title will now link to the custom URL you provided.
就这样。 帖子标题现在将链接到您提供的自定义URL。
It is not necessary to use it for external links only. You can also use it to send users to different posts and pages on your WordPress site.
不必仅将其用于外部链接。 您还可以使用它将用户发送到WordPress网站上的不同帖子和页面。
This method requires you to add code to your WordPress site. You can use this method if you are comfortable with pasting snippets from web into WordPress.
此方法要求您将代码添加到WordPress网站。 如果您愿意将Web片段粘贴到WordPress中,则可以使用此方法。
Simply add this code to your theme’s functions.php file or a site-specific plugin.
只需将此代码添加到主题的functions.php文件或特定于站点的插件中即可 。
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='external_url') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
}
This code looks simply looks for a custom field containing your custom URL. If the post has the custom field, then it outputs the post title linked to your URL.
该代码看起来只是在查找包含您的自定义URL的自定义字段 。 如果帖子具有自定义字段,那么它将输出链接到您的URL的帖子标题。
The next step is to replace your theme’s default display of post title with this function. You will find it in archives.php, content.php, category.php and other templates. It would look something like this:
下一步是用此功能替换主题的主题标题默认显示。 您可以在archives.php,content.php,category.php和其他模板中找到它。 它看起来像这样:
<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
You need to replace it with this code:
您需要用以下代码替换它:
<?php print_post_title() ?>
The code part is over, now you need to add the external URL to the post. Simply edit the post or create a new one. On the post editor page, look for the custom fields meta box.
代码部分已经结束,现在您需要将外部URL添加到帖子中。 只需编辑帖子或创建一个新帖子。 在帖子编辑器页面上,查找自定义字段元框。
If you cannot see the custom fields meta box, then you need to click Screen Options in the top right corner of the screen. This will bring down a menu where you need to check the box next to ‘Custom Fields’.
如果看不到自定义字段元框,则需要单击屏幕右上角的“屏幕选项”。 这将显示一个菜单,您需要在其中选中“自定义字段”旁边的框。
You will find the custom fields meta box below the post editor.
您将在帖子编辑器下方找到自定义字段元框。
Click on ‘Enter New’ and then enter external_url in the ‘Name’ field and the URL you want to add to post title in the ‘Value’ field.
单击“输入新的” ,然后在“名称”字段中输入external_url ,并在“ 值”字段中输入要添加到标题的URL。
You can now save or publish your post. That’s all, your post title will now be linked to the URL you added in the custom field.
您现在可以保存或发布您的帖子。 就是这样,您的帖子标题现在将链接到您在自定义字段中添加的URL。
Next time you need to add a link, you just need to select the external_url custom field from the drop down menu and enter your external link in the value field.
下次您需要添加链接时,只需从下拉菜单中选择external_url自定义字段,然后在value字段中输入您的外部链接。
We hope this article helped you learn how to link to external links from the post title in WordPress. You may also want to see our guide on how to add an external link icon on your WordPress Site.
我们希望本文能帮助您学习如何从WordPress中的帖子标题链接到外部链接。 您可能还想查看有关如何在WordPress网站上添加外部链接图标的指南。
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-link-to-external-links-from-the-post-title-in-wordpress/
wordpress链接跳转