WordPress 文章标题链接到站外链接

WordPress 支持多种文章形式,如果你的主题支持的话,就可以在发布文章的时候,看到如下图所示的选项:

post-formats-wpdaxue_com

我们一般使用“链接”这种文章形式来分享站外链接,点击文章链接时直接访问分享的链接,就像微博一样。要实现这种效果,可以使用倡萌之前推荐过的 Page Links ToWP Post Redirect

也可以通过自定义字段来实现,在主题的 functions.php 添加下面的代码:

/**
 * WordPress 文章标题链接到站外链接
 * https://www.wpdaxue.com/link-post-title-to-external-link.html
 */
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);

然后在发布文章的时候,选择“链接”这种形式,并且添加一个自定义字段 LinkFormatURL 即可:

post-formats-1-wpdaxue_com

如果你希望任何一种文章形式,只要有 LinkFormatURL 这个字段,都重定向到站外链接,可以去除第 6 行的如下代码:

has_post_format('link', $post) &&

来源:

https://www.wpdaxue.com/link-post-title-to-external-link.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32733.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?