dongquxiao8545 2018-01-24 19:58
浏览 36
已采纳

在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.

  • 写回答

1条回答 默认 最新

  • dongzhang1987 2018-01-24 20:32
    关注

    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/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?