首页 > 解决方案 > 如何使用简码显示 WooCommerce 我的帐户仪表板

问题描述

我有一个自定义的 myaccount 页面,我想在其中显示 dashboard.php。我们如何在简码中嵌入dashboard.php模板(我的帐户“仪表板”)?

基于我们如何使用简码显示编辑帐户表单?回答我之前的问题,我在我的functions.php

//[account_dashboard]
add_shortcode('account_dashboard', 'display_account_dashboard');
function display_account_dashboard()
{
    return WC_Shortcode_My_Account::dashboard();
}

但它不起作用

标签: phpwordpresstemplateswoocommerceshortcode

解决方案


更新: 添加了缺少定义的参数 "current_user"

要将模板myaccount/dashboard.php嵌入到短代码中,您将以wc_get_template()这种方式使用该函数:

add_shortcode('account_dashboard', 'display_account_dashboard');
function display_account_dashboard()
{
    return wc_get_template(
        'myaccount/dashboard.php',
        array(
            'current_user' => get_user_by( 'id', get_current_user_id() )
        )
    );

代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。

用法: [account_dashboard]


推荐阅读