WooCommerce:在我的帐户页面中为自定义模板分配端点 [英] WooCommerce: Assigning an endpoint to a custom template in my account pages

查看:33
本文介绍了WooCommerce:在我的帐户页面中为自定义模板分配端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此功能在我的账户"标签列表中添加了一个名为特殊页面"的标签:

add_filter( 'woocommerce_account_menu_items' , 'jc_menu_panel_nav' );函数 jc_menu_panel_nav() {$items = 数组('仪表板' =>__( '仪表板', 'woocommerce'),'订单' =>__( '订单', 'woocommerce'),'下载' =>__( '下载', 'woocommerce' ),'编辑地址' =>__( '地址', 'woocommerce' ),'付款方式' =>__( '付款方式', 'woocommerce' ),'编辑帐户' =>__( '帐户详细信息', 'woocommerce' ),'特殊页面' =>__( 'Special Page', 'woocommerce' ),//我的自定义标签在这里'客户登出' =>__( '退出', 'woocommerce' ),);返回 $items;}

结果如下:

但是链接指向my-account/special-page/,自然会报404错误.

如何将此 URL 分配给名为 special-page.php 的文件?

解决方案

最后我可以用一个片段 为 WooCommerce 的同一个人提供(该页面中有更多提示).对于任何感兴趣的人,请将以下所有代码粘贴到 functions.php 中:

function my_custom_endpoints() {add_rewrite_endpoint('特殊页面', EP_ROOT | EP_PAGES);}add_action('init', 'my_custom_endpoints');函数 my_custom_query_vars( $vars ) {$vars[] = '特殊页面';返回 $vars;}add_filter( 'query_vars', 'my_custom_query_vars', 0 );函数 my_custom_flush_rewrite_rules() {flush_rewrite_rules();}add_action('wp_loaded', 'my_custom_flush_rewrite_rules');

我认为这种方式可以更好地控制菜单的顺序/重命名:

function my_custom_my_account_menu_items( $items ) {$items = 数组('仪表板' =>__( '仪表板', 'woocommerce'),'订单' =>__( '订单', 'woocommerce'),//'下载' =>__( '下载', 'woocommerce' ),//'编辑地址' =>__( '地址', 'woocommerce' ),//'支付方式' =>__( '付款方式', 'woocommerce' ),'编辑帐户' =>__( '编辑帐户', 'woocommerce' ),'特殊页面' =>'特殊页面','客户登出' =>__( '退出', 'woocommerce' ),);返回 $items;}add_filter('woocommerce_account_menu_items', 'my_custom_my_account_menu_items');

在下面的函数中,我包含了该文件以维护一些顺序",但它也承认直接代码.

务必将 special-page.php 文件放在 myaccount 文件夹中.

function my_custom_endpoint_content() {包括woocommerce/myaccount/special-page.php";}add_action('woocommerce_account_special-page_endpoint', 'my_custom_endpoint_content');

<块引用>

重要提示:执行此操作后,请转到仪表板 > 设置 > 固定链接,然后单击保存设置"以刷新重写规则(感谢 @optimiertes)

来源:标签我的帐户页面

This function adds a tab named "Special Page" into "My Account" tab list:

add_filter( 'woocommerce_account_menu_items' , 'jc_menu_panel_nav' );

function jc_menu_panel_nav() {
    $items = array(
        'dashboard'       => __( 'Dashboard', 'woocommerce' ),
        'orders'          => __( 'Orders', 'woocommerce' ),
        'downloads'       => __( 'Downloads', 'woocommerce' ),
        'edit-address'    => __( 'Addresses', 'woocommerce' ),
        'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
        'edit-account'    => __( 'Account Details', 'woocommerce' ),
        'special-page' => __( 'Special Page', 'woocommerce' ), // My custom tab here
        'customer-logout' => __( 'Logout', 'woocommerce' ),
    );

    return $items;
}

That results in this:

But the link points to my-account/special-page/, and naturally gives a 404 error.

How I can assign this URL to a file named special-page.php?

解决方案

Finally I could solve the problem using a snippet provided for the same people of WooCommerce (There are more tips in that page). For anyone interested, paste all the following code in functions.php:

function my_custom_endpoints() {
    add_rewrite_endpoint( 'special-page', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'my_custom_endpoints' );

function my_custom_query_vars( $vars ) {
    $vars[] = 'special-page';

    return $vars;
}

add_filter( 'query_vars', 'my_custom_query_vars', 0 );

function my_custom_flush_rewrite_rules() {
    flush_rewrite_rules();
}

add_action( 'wp_loaded', 'my_custom_flush_rewrite_rules' );

I think this way allows more control to order/renaming the menu:

function my_custom_my_account_menu_items( $items ) {
    $items = array(
        'dashboard'         => __( 'Dashboard', 'woocommerce' ),
        'orders'            => __( 'Orders', 'woocommerce' ),
        //'downloads'       => __( 'Downloads', 'woocommerce' ),
        //'edit-address'    => __( 'Addresses', 'woocommerce' ),
        //'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
        'edit-account'      => __( 'Edit Account', 'woocommerce' ),
        'special-page'      => 'Special Page',
        'customer-logout'   => __( 'Logout', 'woocommerce' ),
    );

    return $items;
}

add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );

In the following function I included the file to maintain some "order", but it also admits direct code.

Be sure to place the special-page.php file in the myaccount folder.

function my_custom_endpoint_content() {
    include 'woocommerce/myaccount/special-page.php'; 
}

add_action( 'woocommerce_account_special-page_endpoint', 'my_custom_endpoint_content' );

Important: Once did this, go to Dashboard > Settings > Permalinks and click "Save Settings" in order to flush rewrite rules (thanks @optimiertes)

Source: Tabbed My Account page

这篇关于WooCommerce:在我的帐户页面中为自定义模板分配端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆