2017-05-30 80 views
0

我正在一個WordPress網站上工作,其中服務頁面是一個長頁面,每個部分都有錨點。WordPress頁面上的指定錨點的漂亮網址

www.mysite.com/services/#human-resources按預期跳轉到人力資源部分。

在.htaccess文件中是否有任何方法可以使諸如www.mysite.com/services/human-resources/之類的url直接跳轉到錨點?

我試過幾個不同的重寫規則,我已經得到最接近的是:

RewriteRule services/human-resources services/#human-resources [NE,L,R=301] 

但是,這顯示了其失敗的目的URL中的#。刪除R=301只會導致它無法執行任何操作。

謝謝。

回答

0

我一直在類似的網站上工作,這就是我解決這個問題的方法。

首先將此動作添加到functions.php中,女巫將用戶重定向到頁面上的適當錨點。

function url_handler(){ 
    $path = substr($_SERVER['REQUEST_URI'], 1); //get the path minus the '/' 
    if(is_page() || is_404()){ // do you own conditional tags 
    wp_redirect(home_url("#" . $path)); //redirect to the #anchor 
    exit(); 
    } 
} 
add_action('template_redirect', 'url_handler'); 

接下來你需要做一個javascirpt處理器女巫獲取頁面(與錨的網址)的網址,並作出適當的情況下,滾動到該節。事情是這樣的:

var hash = window.location.hash; 
hash = hash.replace('#',''); 
hash = hash.replace('/',''); 
/* do stuff here */ 

希望這有助於