【问题标题】:wp_footer action hook generating error in my WordPress fileswp_footer 操作挂钩在我的 WordPress 文件中生成错误
【发布时间】:2016-10-20 05:14:16
【问题描述】:

我正在使用海狸构建器主题和海狸构建器插件,目前我正在创建自己的新模块并尝试将我的所有脚本代码发送到页脚,因为我的代码如下所示:

<?php function gt_footer_script(){?>
    <script>
        jQuery(document).ready(function($) {
        "use strict";

        if ($('#gt-lightgallery_v<?php echo $gt_filter_counter; ?>').length) {
            $('#gt-lightgallery_v<?php echo $gt_filter_counter; ?>').lightGallery({
                selector: '.gt-gallery-box',

            });
        }
        });
    </script>
    <?php } add_action('wp_footer', 'gt_footer_script');?>

我的函数是 gt_footer_script 但是一旦我运行我的代码,它说不能重新声明函数gt_footer_script 它已经在第 110 行声明,而它的第一个函数创建了我,否则我从来没有使用过这个函数我当前的插件,但在运行我的代码时仍然存在问题。

【问题讨论】:

  • 这段代码在哪个文件中?该文件是否被包含两次?
  • 不,这个文件根本没有包含两次,它被用于海狸构建器自定义模块扩展的 frontend.php 文件中......它是两次,那么对于我在我的所有其他函数中使用的函数呢?插件???
  • 你试过改函数名吗?
  • 是的,但它是无效的

标签: wordpress


【解决方案1】:

您应该只检查它是否已经加载过一次。您的插件可能会在调用时加载这些操作,然后在调用您的模块时再次加载您的函数。喜欢:

<?php 
// We check first if the function wasn't declared earlier : 
if(!function_exists('gt_footer_script')) :
// Then we load it
function gt_footer_script(){
?>
<script>
    jQuery(document).ready(function($) {
    "use strict";

    if ($('#gt-lightgallery_v<?php echo $gt_filter_counter; ?>').length) {
        $('#gt-lightgallery_v<?php echo $gt_filter_counter; ?>').lightGallery({
            selector: '.gt-gallery-box',

        });
    }
    });
</script>
<?php 
} 
endif;
// If the function already exists, no issue to run this code : 
add_action('wp_footer', 'gt_footer_script');
?>

【讨论】:

  • 哦,该死的……我怎么能匆忙忘记所有那些PHP……非常感谢您指出这一点:)
  • 没问题! ;)
猜你喜欢
  • 2015-02-19
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多