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 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测