从WordPress中的另一个函数访问变量函数

时间:2013-10-10 16:54:00

标签: php wordpress

有问题,我无法理解我做错了什么..

我想获得WordPress中其他功能的功能值。

此代码替换了代码的某些部分..

我想得到参数变量字的值(它需要去$ attr ['words'])然后使用其他函数(new_quote)。

    <?php
    /*
    * Plugin Name: Random Quotes
    */

    function random_quote($atts) {
        extract( shortcode_atts( array(
        'path' => plugin_dir_path(__FILE__).'quotes.txt',// default, if not set
        'label_new' => 'New Quote',
        'words' => 'no'   // yes or no 
        ), $atts ) );

        $temp = $attr['words']; // no
        ...

    }

    add_shortcode('randomquotes','random_quote');


    function new_quote(){
    global $temp;  // NULL
    /*
    global $attr;
    $temp = $attr['words']; // again NULL
    */
        ...

        if($temp == "no") {
        ...
        }
    }

   ...

?>

我做错了什么?也许只是无法得到这个变量的值?

1 个答案:

答案 0 :(得分:2)

看起来你需要在random_quote()函数中声明全局$ temp。现在,random_quote()正在使用$ temp的本地版本,当函数完成时会丢失。

编辑:这是一个示例代码段

<?php
function test() {
  global $temp;
  $temp = 'no';
}
function my_test() {
  global $temp;

  var_dump($temp);
}

test();
my_test();
?>
相关问题