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

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

post-formats-wpdaxue_com

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

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

1
2
3
4
5
6
7
8
9
10
11
/**
 * 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);

/**
* 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 行的如下代码:

1
has_post_format('link', $post) &&

has_post_format(‘link’, $post) &&

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号