小編最近發現很多新手在找 WordPress 鏈接轉跳功能,網上也有很多各種各樣的教程,不過大多數教程的代碼都差不多,基本上都是要新建一個轉跳文件或者是創建一個頁面模板來做轉跳中間頁的。那麼今天小編就教大家兩種最簡單最方便的方法來給 WordPress 添加一個站內鏈接跳轉到外部鏈接的功能。
方法一:直接以 https://www.weixiaoduo.com/?go=http://www.baidu.com 這種形式轉跳的方法。
add_action('wp_head','wxd_gourl');
function wxd_gourl(){
global $pagenow;
if(is_home&&$pagenow=='index.php'){
$location=$_GET['go'];
if($location!=""){
wp_redirect(esc_url_raw($location),302);
exit;
}
}
}
直接將以上代碼家到主題的 functions.php 文件即可實現 WordPress 鏈接轉跳功能。
方法二:利用 WordPress 自定義域 (自定義字段來轉跳),此方法適合淘寶客站點用來隱藏淘寶鏈接。
add_action('wp_head','wxd_gometa');
function wxd_gometa(){
global $pagenow;
if(is_home&&$pagenow=='index.php'){
$postID=$_GET['goid'];
if($postID){
$postID=(int)$postID;
$location=get_post_meta($postID,'wxd_gometa',true);
if($location!=""){
wp_redirect(esc_url_raw($location),302);
exit;
}
}
}
}
首先將以上代碼加到主題的 functions.php 文件中,然後在編輯文章添加一個名稱為 wxd_gometa 的自定字段,值為你需要轉跳的目標頁鏈接,如下圖示:


然後就可以通過 https://www.weixiaoduo.com/?goid=123 這種形式來進行轉跳咯,當然鏈接裏的 123 你要替換成你文章的 ID 。