在wp_nav_menu Wordpress + Symfony中更改URL

问题描述:

In my symfony project I'm trying to use wordpress for free user-friendly content manager. My problem is, when I'm trying to take Wordpress menu, like:

    $menu = wp_nav_menu( array(
        'menu_id'   => 'top-menu',
    ) );

Ofcourse menu items links to URL's like:

/wordpressfolder/page-item

And the point is: how to change it to:

/somethinganother/page-item

?

I want to do that, because I've changed the standard CMS routing, so I need to be consequent, that the project can look professional.

Ofcourse I'dont mind the js option because it's obvious, but I want to do it on server site if it is possible.

在我的symfony项目中,我正在尝试将wordpress用于免费的用户友好型内容管理器。 我的问题是,当我尝试使用Wordpress菜单时,如: p>

  $ menu = wp_nav_menu(array(
'menu_id'=>'top-menu'  ,
)); 
  code>  pre> 
 
 

Ofcourse菜单项链接到URL,如: p>

  / wordpressfolder / page-  item 
  code>  pre> 
 
 

重点是:如何将其更改为: p>

  / somethinganother / page-item \  n  code>  pre> 
 
 

? p>

我想这样做,因为我已经改变了标准的CMS路由,所以我需要成为结果 ,该项目看起来很专业。 p>

Ofcourse我不介意js选项,因为它很明显,但我想在服务器网站上尽可能这样做。 p> \ n div>

You could use one of the filters WordPress comes with. The following code example shows a way you could use.

function mmn_main_item_rewrite( $items, $args ) 
{
    foreach ( $items as $item ) {
        $item->url = str_replace( 'wordpressfolder', 'somethinganother', $item->url );
    }

    return $items;
} 
add_filter( 'wp_nav_menu_objects', 'mmn_main_item_rewrite', 10, 2 );

Just add this piece of code in the functions.php file in your active WordPress theme folder. Always use WordPress child themes for individual changes.

More details about the wp_nav_menu_objects hook in the WordPress documentation: https://developer.wordpress.org/reference/hooks/wp_nav_menu_objects/