简体   繁体   English

wordpress functions.php

[英]wordpress functions.php

I'm having issues with the functions.php file with variables 我在带有变量的functions.php文件中遇到问题

        $prev_dept = 0;
        $comment_count = 0;
        $comment_index = 0;
        function setCommentCount($size){
            $comment_count = $size;
        }        
        function flowhub_comment($comment, $args, $depth) {
            $comment_index ++;            

            if($depth > 1) {
                $line = true;
            }
            echo '$prev_dept:' . $prev_dept.'<br>';
        }

I can't access $comment_index so I can't set nor get it from within a function. 我无法访问$ comment_index,因此无法在函数中进行设置或获取。 What should I do to fix this? 我应该怎么做才能解决这个问题?

Yours truthfully 诚实地

$comment_index is not within the scope of the functions, you need to use global . $comment_index不在函数范围内,您需要使用global More details on scoping in PHP . 有关PHP作用域的更多详细信息

The way functions.php works is not just a plain include, try GLOBAL it might help. functions.php的工作方式不仅包含在内,尝试使用GLOBAL可能会有所帮助。

function setCommentCount($size){
    global $comment_count;
    $comment_count = $size;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM