doudou5101 2018-11-22 10:49
浏览 59
已采纳

重新声明子主题中的函数

P.S. Hi Admin, I tried looking for the solution but could not see the same issue anywhere!

Hi all,

I have a child theme. I have a function which is wrapped in if !function_exists as below. The file in is includes folder. The function is in wp-content/themes/themename/includes/alias-function.php

 if (!function_exists('jem_render_buy_fee ')) {
function jem_render_buy_fee() {
    $fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
    if($fee){
        ?>
        <div class="jem-commission-fee">
            <span><?php _e($fee.'% commission fee included', 'themes'); ?></span>
        </div>
        <?php
    }
}
}

Because it is in includes folders but wrapped in function_exists, so I declare the function in functions.php as below. This file is in wp-content/themes/themename-child/functions.php

function jem_render_buy_fee() {
    $fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
    if($fee){
        ?>
        <div class="jem-commission-fee">
            <span><?php _e($fee.'% GST inclusive', 'themes'); ?></span>
        </div>
        <?php
    }
}

I am getting the error:

Your PHP code changes were rolled back due to an error on line 1873 of file wp-content/themes/themename/includes/alias-function.php. Please fix and try saving again.

Cannot redeclare jem_render_buy_fee() (previously declared in wp-content/themes/themename-child/functions.php:215)

Why am I getting this? The error is wrapped in function_exist.

I can see its loading the function.php, but then it should ignore the existing one in the file.

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • douxuzui4590 2018-11-22 11:23
    关注

    In your check for function existence, there's a space behind the function name. !function_exists('jem_render_buy_fee ')

    Without this space it should work fine if the order af calling code is correct.

    First Call:

    function jem_render_buy_fee() {
        $fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
        if($fee){
            ?>
            <div class="jem-commission-fee">
                <span><?php _e($fee.'% GST inclusive', 'themes'); ?></span>
            </div>
            <?php
        }
    }
    

    Then call:

    <?php
    if (!function_exists('jem_render_buy_fee')) { //Space removed from 'jem_render_buy_fee '
        function jem_render_buy_fee() {
            $fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
            if ($fee): ?>
                <div class="jem-commission-fee">
                    <span><?php _e($fee.'% commission fee included', 'themes'); ?></span>
                </div>
            <?php endif;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料