博客文章标题链接到自定义网址链接 —— WordPress教程

2022年1月7日16:58:55 发表评论 2,280
WordPress

支持多种文章形式,相信不少博主遇到过需要把wordpress文章的标题链接到自定义的URL地址的情况,点击文章链接时不是直接访问文章详情页面,而是直接访问分享的链接网页,就像微博一样。要实现这种效果,可以使用插件:Page Links To 或 WP Post Redirect来实现,但是为了这个小功能再装一个插件就有点小题大做了。

 

博客文章标题链接到自定义网址链接 —— WordPress教程

 

方式一:

其实我们也可以通过给文章添加自定义字段来实现,在主题的 functions.php 添加下面的代码:(文件地址:网站根目录/wp-content/themes/主题名文件夹/functions.php)

/**
 * WordPress 文章标题链接到站外链接
 */
function link_format_url($link, $post) {
     if (has_post_format('link', $post) && get_post_meta($post->ID, 'LinkFormatURL', true)) {
          $link = get_post_meta($post->ID, 'LinkFormatURL', true);
     }
     return $link;
}
add_filter('post_link', 'link_format_url', 10, 2);

 

方式二:

依次打开wordpress后台的外观/主题编辑器/模板函数(functions.php),添加添加下面的代码:

/**
 * WordPress 文章标题链接到站外链接
 */
function link_format_url($link, $post) {
     if (has_post_format('link', $post) && get_post_meta($post->ID, 'LinkFormatURL', true)) {
          $link = get_post_meta($post->ID, 'LinkFormatURL', true);
     }
     return $link;
}
add_filter('post_link', 'link_format_url', 10, 2);

 

使用教程

新建wordpress文章,在文章末尾的自定义字段里面的名称选择:LinkFormatURL ,值:输入你需要文章标题跳转的url地址,正常发布文章即可实现上述功能。

 

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如果喜欢,请打赏支持本站,谢谢大家!

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: