WordPress 支援多種文章形式,如果你的主題支援的話,就可以在發佈文章的時候,看到如下圖所示的選項:
我們一般使用“鏈接”這種文章形式來分享站外鏈接,點擊文章鏈接時直接訪問分享的鏈接,就像微博一樣。要實現這種效果,可以使用之前推薦過的 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 即可:
如果你希望任何一種文章形式,隻要有 LinkFormatURL 這個字段,都重定向到站外鏈接,可以去除第 6 行的如下代碼:
1 |
has_post_format('link', $post) && |
has_post_format(‘link’, $post) &&