0
function partners($atts ) {
    extract(shortcode_atts(array(  
            'ids' => null,
            'extra_options' => 'something' <----------------- in wordpress I can read this value using local $extra_options 
    ), $atts));  
global $extra_options; <----------------- trying to change local var to global

function print_partners_scripts() {
    global $extra_options; <----------------- reading above variable
    echo '<script type="text/javascript">' . "\n";
    echo 'jQuery(document).ready( function() {'. "\n";
    echo '  $(".partners-slider").bxSlider({
        slideWidth: 924,
        auto: 0,
        autoStart: 0,
        moveSlides: 1,
        minSlides: 3,
        maxSlides: 8,
        pager: false,
        controls: false,
        slideMargin: 5,
        ' . $extra_options . ' <----------------- var is empty
     });' . "\n";
    echo '});' . "\n";
    echo '</script>' . "\n";
} 
    add_action( 'wp_footer', 'print_partners_scripts' );

    $ids = explode( ',', $ids );
    $output = '<div class="ps-wrap"><div class="partners-slider">';
    foreach($ids as $id) {    
    $img_attr = wp_get_attachment_image_src( $id, 'full' );
    $output .= '<div class="pslide"><img src="' . $img_attr[0] . '" /></div>';    
}
    $output .= '</div></div>';

    return $output;  
}  

嗨,我正在尝试读取 print_partners_scripts() 中的 var $extra_options。该变量在 partners() 函数中设置。我试图让它全球化并简单地在某个地方使用它,但我想我做错了什么;)

提前致谢 !

4

2 回答 2

1

首先,PHP 不支持您尝试使用嵌套函数的方式。

你可以这样写:

function outer() { function inner() {} }
outer();

但所发生的只是在outer();执行时,该inner()函数被声明为普通函数。所以代码与此完全相同:

function outer() {}
function inner() {}
outer();

其次,PHP 中的变量(除非带有类名或对象名的前缀)始终作用于当前函数。global关键字将对全局变量的引用导入当前函数的作用域;它不能用于导出已定义的变量。

通常最好只global在函数的开头使用关键字,以导入该函数所需的所有全局变量。更好的是,不要使用全局变量,因为它们会导致难以理解和调试的“意大利面条代码”。

如果您在运行global 之前extract声明变量,这将起作用,但我强烈建议您不要使用任何一个功能

function foo_with_too_much_magic()
{
    // Import global variable. Very hard to track where this came from.
    global $some_var;
    // Let's assume this array comes from somewhere and isn't hard-coded
    $some_array = array('some_var' => 'some_value');
    // Export variables from an array. This is like telling PHP to write different code each time it runs, with different variable names.
    extract( $some_array );
}
foo_with_too_much_magic();
var_dump($some_var);

这是上面的版本,没有不鼓励的功能:

function foo_with_no_magic()
{
    // Let's assume this array comes from somewhere and isn't hard-coded
    $some_array = array('some_var' => 'some_value');
    // You know which variable you want, so don't need the magic "export"
    // Note that you don't have to call it $some_var
    $some_var = $some_array['some_var'];

    // Now you have the variable, you can manipulate it, pass it to another function, or return it
    // In fact, you could also return $some_array['some_var'] directly, without the extra assignment
    return $some_var;
}

// This variable name no longer needs to be the same as what was used in the foo_with_no_magic() function
$some_var = foo_with_no_magic();
var_dump($some_var);
于 2013-10-02T18:09:34.793 回答
0

这是一个将代码放入类格式的示例,我会参考这个方向,学习更多 PHP 的 OOP 实践(http://php.net/manual/en/language)可能会很有用。 oop5.php):

#1) Get the data you wish to pass into your function.
$data = "TEST";  
get_partners($data);

#2) Call your function.
function get_partners($atts) {
    //Extract using ($att) passed in from your call.
    //The shortcode_atts function should be accessible by the file containing this function.
    extract(shortcode_atts(array(
    'ids' => null,
    'extra_options' => 'something' //in wordpress I can read this value using local $extra_options
    ), $atts));
    //Create a new class element that will build your data for your and allow you to pass in your variable on the fly.
    $p = new partners();
    $p->extra_options= $atts; //Pass the variable here.
    $p->print_partners_scripts();
}

#3) Define Class here.
class partners {
    var $extra_options;

    public function print_partners_scripts()
    {
        $output = '<script type="text/javascript">' . "\n";
        $output .= 'jQuery(document).ready( function() {'. "\n";
        $output .= '  $(".partners-slider").bxSlider({
        slideWidth: 924,
        auto: 0,
        autoStart: 0,
        moveSlides: 1,
        minSlides: 3,
        maxSlides: 8,
        pager: false,
        controls: false,
        slideMargin: 5,
        ' . $this->extra_options . '
        });' . "\n";
        $output .= '});' . "\n";
        $output .= '</script>' . "\n";            
        $output .= $this->additional_data();
        echo $output;
    }

    protected function additional_data()
    {
        add_action( 'wp_footer', 'print_partners_scripts' );
        $ids; #Where is this defined?
        $ids = explode( ',', $ids );
        $output = '<div class="ps-wrap"><div class="partners-slider">';

        foreach($ids as $id)
        {
            $img_attr = wp_get_attachment_image_src( $id, 'full' );
            $output .= '<div class="pslide"><img src="' . $img_attr[0] . '" /></div>';
        }

        $output .= '</div></div>';
        return $output;
    }
}
于 2013-10-02T18:37:46.720 回答