Wordpress 如何在wp_nav_菜单中修改url?
我有: 这给了我:Wordpress 如何在wp_nav_菜单中修改url?,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我有: 这给了我: wp_nav_menu(array( 'theme_location' => 'header-menu', 'depth' => 1, 'container' => 'div', 'link_before' => '', 'link_after' => '', ) ); 致: 怎么做?我想我应
wp_nav_menu(array(
'theme_location' => 'header-menu',
'depth' => 1,
'container' => 'div',
'link_before' => '',
'link_after' => '',
)
);
致:
怎么做?我想我应该试试使用PHP将HTML解析为字符串并替换href属性的值
"javascript:myfunc('http://link*')";
$content='1!'
-
-
-
';
//新的Dom对象
$dom=新的DomDocument;
//将$content作为字符串加载
$dom->loadHTML($content);
//只得到一个元素
$elements=$dom->getElementsByTagName('a');
//循环遍历每个a元素并获取其href值
对于($n=0;$n<$elements->length;$n++){
$item=$elements->item($n);
//变老
$old_href=$item->getAttribute('href');
//新href val
$new\u href='javascript:myfunc(\'.$old\u href.\'');
//用新的替换旧的href
$content=str_replace($old_href,$new_href,$content);
}
//打印结果
echo$内容;
您能使用jQuery吗?还是必须在服务器端完成?我尝试过使用过滤器,但它正在剥离JavaScript。将所有这些代码包装在一个函数中,对于这种情况和其他类似情况,这是一个非常好的解决方案+1@brasofilo啊,这很有趣。在我看来,指向您的解决方案的链接非常合理-我想知道为什么它会剥离JS?我相信它在wp includes/nav menu.php中的链接上运行esc\u url\u raw
。有一个动作挂钩,wp\u update\u nav\u menu\u item
,可能有用,但我将把它放到另一个场合。
"http://link*"
"javascript:myfunc('http://link*')";
$content = '
<ul>
<li class="page_item page-item-1">
<a href="http://link1">link1</a>
</li>
<li class="page_item page-item-2">
<a href="http://link2">link2</a>
</li>
<li class="page_item page-item-3">
<a href="http://link3">link3</a>
</li>
</ul>
';
// New Dom Object
$dom = new DomDocument;
// Load $content as string
$dom->loadHTML($content);
// Get only a elements
$elements = $dom->getElementsByTagName('a');
// Loop through each a element and get it's href value
for ($n = 0; $n < $elements->length; $n++) {
$item = $elements->item($n);
// Get old href val
$old_href = $item->getAttribute('href');
// New href val
$new_href = 'javascript:myfunc(\''.$old_href.'\')';
// Replace old href with new
$content = str_replace($old_href,$new_href,$content);
}
// Print results
echo $content;