我在一个WordPress网站上工作,其中的服务页面是一个很长的页面,每个部分都有锚点。
如预期的那样,www.mysite.com/services/# Human -resources跳转到人力资源区。
在.htaccess文件中,有没有办法让一个url直接跳转到锚点?
我尝试了几种不同的重写规则,我得到的最接近的是:
RewriteRule services/human-resources services/#human-resources [NE,L,R=301]但是这显示了url中的#,这违背了它的目的。删除R=301只会导致它什么也不做。
谢谢。
发布于 2017-05-30 17:59:30
我一直在一个类似的网站上工作,这就是我如何解决这个问题的。
首先将此操作添加到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处理程序,它获取页面的url (带有锚点的url ),并生成适当的事件来滚动到该部分。如下所示:
var hash = window.location.hash;
hash = hash.replace('#','');
hash = hash.replace('/','');
/* do stuff here */希望这能有所帮助
https://stackoverflow.com/questions/44251694
复制相似问题