我想在 WooCommerce 我的帳戶導航中僅向特定用戶角色顯示訂閱鏈接。但我不知道如何以這種方式更改導航。我找到了更改菜單項順序的解決方案。我找到了一個向導航添加自定義鏈接的解決方案。但不幸的是,我無法將這兩者組合成一個工作片段。這是更改順序的代碼: function my_account_menu_order() { $menuOrder = array( 'orders' => __( 'Orders', 'woocommerce' ), 'downloads' => __( 'Download', 'woocommerce' ), 'edit-address' => __( 'Addresses', 'woocommerce' ), 'edit-account' => __( 'Account Details', 'woocommerce' ), 'customer-logout' => __( 'Logout', 'woocommerce' ), 'dashboard' => __( 'Dashboard', 'woocommerce' ), ); return $menuOrder; } add_filter ( 'woocommerce_account_menu_items', 'my_account_menu_order' );因為鏈接在一個數組中,所以我不能if/else用來檢查用戶角色。這是向導航添加自定義鏈接的示例:(來自此處:https ://rudrastyh.com/woocommerce/my-account-menu.html#add_with_url )add_filter ( 'woocommerce_account_menu_items', 'misha_one_more_link' );function misha_one_more_link( $menu_links ){ // we will hook "anyuniquetext123" later $new = array( 'anyuniquetext123' => 'Gift for you' ); // or in case you need 2 links // $new = array( 'link1' => 'Link 1', 'link2' => 'Link 2' ); // array_slice() is good when you want to add an element between the other ones $menu_links = array_slice( $menu_links, 0, 1, true ) + $new + array_slice( $menu_links, 1, NULL, true ); return $menu_links;}add_filter( 'woocommerce_get_endpoint_url', 'misha_hook_endpoint', 10, 4 );function misha_hook_endpoint( $url, $endpoint, $value, $permalink ){ if( $endpoint === 'anyuniquetext123' ) { // ok, here is the place for your custom URL, it could be external $url = site_url(); } return $url;}但是訂閱端點已經存在。所以我不知道如何以這種方式添加它。有任何想法嗎?
1 回答

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
抱歉,我找到了解決方案。
以下代碼可以解決問題:
add_filter ( 'woocommerce_account_menu_items', 'misha_one_more_link' );
function misha_one_more_link( $menu_links ){
// we will hook "anyuniquetext123" later
$new = array( 'subscriptions' => __( 'Subscriptions', 'woocommerce' ), );
// or in case you need 2 links
// $new = array( 'link1' => 'Link 1', 'link2' => 'Link 2' );
// array_slice() is good when you want to add an element between the other ones
$menu_links = array_slice( $menu_links, 0, 1, true )
+ $new
+ array_slice( $menu_links, 1, NULL, true );
return $menu_links;
}
我只需要添加現有的端點。
- 1 回答
- 0 關注
- 98 瀏覽
添加回答
舉報
0/150
提交
取消