php - 仅在 WooCommerce 我的帐户仪表板中添加自定义 html 内容

标签 php wordpress woocommerce account woocommerce-subscriptions

我将 WordPress 与 WooCommerce 和 WooCommerce 订阅插件一起使用,并且根据我的要求工作正常。

现在我想在登录系统后在 /my-account/ 页面中添加我的自定义 div 即 ( http://www.example.com/my-account )。

enter image description here

所以我研究并发现了这个页面 /wp-content/plugins/woocommerce/templates/myaccount/my-account.php 并且该页面有以下代码。

<?php 
defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_account_navigation' ); ?>

<div class="woocommerce-MyAccount-content">
    <?php       
        do_action( 'woocommerce_account_content' );
    ?>
</div>

因此我添加了类似这样的代码

<div>Test</div>

do_action( 'woocommerce_account_content' ); 之后,它会出现在所有页面中,例如 my-account/orders/ , /my-account/downloads/ 等..我不想要它。

enter image description here

enter image description here

我怎样才能只在 /my-account/ 页面显示这个?此外,我怎样才能通过复制我的一个事件主题来覆盖此页面,这样我就不需要对插件的核心文件进行更改?

最佳答案

要使其仅在“我的帐户”仪表板中显示而不覆盖任何模板文件,您可以改用以下 Hook 函数:

add_action( 'woocommerce_account_dashboard', 'custom_account_dashboard_content' );
function custom_account_dashboard_content(){
    echo '<div>' . __("Test", "woocommerce") . '</div>';
}

代码进入事件子主题(或事件主题)的 functions.php 文件。经过测试并有效。

自定义 html 内容仅按预期添加到我的帐户仪表板(没有覆盖任何模板文件):

enter image description here

注意:这需要替换回 WooCommerce 原来的相关模板文件。

关于php - 仅在 WooCommerce 我的帐户仪表板中添加自定义 html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67109228/

相关文章:

javascript - Jquery PrettyPhoto 从 URL 中删除#prettyphoto

php - 将产品元移动到附加信息选项卡

byte - 从 php.ini 中的速记字节表示法获取字节值

php - [if ( !defined( 'ABSPATH' ) )] 是什么意思

php - 如何管理多标签/多分类文章/帖子的数据库和查询

php - 复杂的元查询不起作用

woocommerce - 更改产品页面上的 'choose an option' 文本

php - 限制 Woocommerce 结账时使用的优​​惠券数量

php - PHP 中的异步 Web 服务

php - 无法在 PHP 中调用 MYSQL 过程