Php 在“我的帐户”菜单中的注销项上方添加自定义项

Php 在“我的帐户”菜单中的注销项上方添加自定义项,php,wordpress,woocommerce,menuitem,hook-woocommerce,Php,Wordpress,Woocommerce,Menuitem,Hook Woocommerce,我已经在谷歌上搜索了一段时间,但我似乎找不到答案,也找不到其他有同样问题的人。我正在尝试在“注销”项上方添加一个菜单项“我的文件”。 以下是我使用的代码: // Add Menu Item function my_account_menu_items( $items ) { return array_merge( array_slice( $items, 0, count( $items ) - 1 ), array( 'my-files' => '

我已经在谷歌上搜索了一段时间,但我似乎找不到答案,也找不到其他有同样问题的人。我正在尝试在“注销”项上方添加一个菜单项“我的文件”。 以下是我使用的代码:

// Add Menu Item
function my_account_menu_items( $items ) {
    return array_merge(
        array_slice( $items, 0, count( $items ) - 1 ),
        array( 'my-files' => 'My Files' ),
        array_slice( $items, count( $items ) - 1 )
    );
}
add_filter( 'woocommerce_account_menu_items', 'my_account_menu_items', 10, 1);
我尝试过用不同的方式调用过滤器,没有最后两个参数。我也试过这样做

function my_account_menu_items( $items ) {
    $items['my-files'] = 'My Files';
    return $items;
}

您应该尝试以下方法:

add_filter( 'woocommerce_account_menu_items', 'add_item_to_my_account_menu', 30, 1 );
function add_item_to_my_account_menu( $items ) {
    $new_items = array();
    // Loop throu menu items
    foreach( $items as $key => $item ){
        if( 'customer-logout' == $key )
            $new_items['my-files'] = __( 'My Files' );

        $new_items[$key] = $item;
    }
    return $new_items;
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作


这是经过测试并正常工作的:

function custom_my_account_menu_items( $items ) {

    $new_items = array();
    foreach($items as $key => $value){
        if($key != 'customer-logout'){
            $new_items[$key] = $value;
        }else{
            $new_items['my-files'] = 'My Files';
            $new_items[$key] = $value;
        }
    }
    return $new_items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );

您可以通过取消设置登录名并将其添加回最后一件事来确保登录名保持在底部

//keep logout the last menu item
function custom_my_account_menu_items( $items ) {
    unset($items['customer-logout']);
    $items['customer-logout'] = 'Logout';
    return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items', 999 );

我试过了,但还是不起作用。代码在我的functions.php中,我应该在上面的帖子中提到它。我还试图清除我的浏览器缓存,但没有任何帮助。@TarikDruskic所以你的主题(或某个插件)肯定涉及到这个问题,以及它们各自的自定义设置。这段代码非常经典,几乎可以在任何地方使用。因此,您可以联系您的主题支持线程…