我找到了改变wp_nav_menu
使用wp_get_nav_menu_items-filter
. 下面是一个比较完整的示例:
class ModifyLinkFilter {
protected $_prio = 10;
protected $_args;
public function __construct($addargs = array(), $prio = 10) {
$this->_args = $addargs;
$this->_prio = $prio;
if(!empty($addargs)) {
$this->register();
}
}
public function register() {
add_filter(\'wp_get_nav_menu_items\',
array($this, \'on_nav_items\'), $this->_prio, 3);
}
public function unregister() {
remove_filter(\'wp_get_nav_menu_items\',
array($this, \'on_nav_items\'), $this->_prio, 3);
}
public function on_nav_items($items, $menu, $args) {
foreach($items as $item) {
if(!empty($item->url)) {
$item->url = self::modifyUrlSimple($item->url, $this->_args);
}
}
return $items;
}
public static function modifyUrlSimple($url, $query) {
$url .= strchr($url, \'?\') === false ? \'?\' : \'&\';
$url .= http_build_query($query);
return $url;
}
}
// You can use the class like that
$language = "de";
$args = array(\'lang\' => $language, \'foo\' => \'bar\');
$linkfilter = new ModifyLinkFilter($args);
wp_nav_menu();
$linkfilter->unregister();
这将修改导航菜单中的每个项目。因此,如果您有一个外部链接,它也会被更改。
此外,修改URL并不像看起来那么容易。项目的URL可以是/blabla?myvalue=5#anchor
看起来像是/blabla?myvalue=5#anchor&lang=de&foo=bar
修改后。