5

wordpress实现文章标题链接到自定义网址链接

 2 years ago
source link: https://www.huhexian.com/33755.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

wordpress实现文章标题链接到自定义网址链接

2022-03-2316:32:43评论923字

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

wordpress实现文章标题链接到自定义网址链接

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

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

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

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK