WordPress ,本地锚点而不是 wp_nav_menu 的完整链接
我正在尝试将 href 更改为本地锚点。我的想法是使用 preg_replace 来剥离 home_url();
而不是......
<a href="http://example.com/menu-item/">Menu Item</a>
...我想
<a href="#menu-item/">Menu Item</a>
到目前为止创建我的代码:
$menu_one = wp_nav_menu(
array(
'echo' => 0,
'container' => false,
'menu_class' => '',
'menu_id' => '',
'theme_location' => 'main-menu'
)
);
//get home url
$homeurl = home_url();
$menu_one = preg_replace( $homeurl, '#', $menu_one );
echo '<ul id="main-menu">' . $menu_one . '</ul>';
我目前收到错误 preg_replace(): 分隔符不能是字母数字或反斜杠;任何帮助将不胜感激(我的 php 知识非常有限,也许还有一种更优雅的方法来做到这一点)
I'm trying to change the href to a local anchor. My idea was to use preg_replace to strip the home_url();
Instead of....
<a href="http://example.com/menu-item/">Menu Item</a>
...I'd like to create
<a href="#menu-item/">Menu Item</a>
My code so far:
$menu_one = wp_nav_menu(
array(
'echo' => 0,
'container' => false,
'menu_class' => '',
'menu_id' => '',
'theme_location' => 'main-menu'
)
);
//get home url
$homeurl = home_url();
$menu_one = preg_replace( $homeurl, '#', $menu_one );
echo '<ul id="main-menu">' . $menu_one . '</ul>';
I currently get the error preg_replace():
Delimiter must not be alphanumeric or backslash; Any help would be appreciated (my php knowledge is quite limited and also maybe there is a more elegant way to do this)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
$homeurl
包装在/$homeurl/
等分隔符中,使其成为有效的正则表达式模式。此外,转义 URL 中的某些字符也很重要。
You need to make your
$homeurl
into a valid regex pattern by wrapping it in a delimiter like/$homeurl/
.Also, it is vital to escape some of the characters in the URL.