如何从另一个函数初始化 $id? [英] How to initialize $id from another function?

查看:26
本文介绍了如何从另一个函数初始化 $id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 $id 来回显在其他函数中输入的 id.这是一个使用 id 的示例函数:

I would like to use $id to echo the id entered in other functions. Here is an example function using id:

                function nl_register_ga_code() {
                    // Validation callback
                    register_setting( 'nl_theme_options', 'nl_theme_options', 'nl_validate_settings' );
                    // Add setting to section
                    add_settings_section( 'nl_footer_section', 'Footer', 'nl_display_footer_section', 'nl_theme_options.php' );
                    // Create textarea field
                    $field_args = array(
                        'type'      => 'textarea',
                        'id'        => 'nl_ga_code',
                        'name'      => 'nl_ga_code',
                        'desc'      => 'Paste Google Analytics code.',
                        'std'       => '',
                        'label_for' => 'nl_ga_code'
                    );
                    // Label
                    add_settings_field( 'label_ga_code', 'Google Analytics Code', 'nl_display_setting', 'nl_theme_options.php', 'nl_footer_section', $field_args );
                }
                // Registers the setting
                add_action( 'admin_init', 'nl_register_ga_code' );

我将使用该变量的函数是:

The function I will be using the variable is this one:

        function nl_display_setting ( $args ) {
            extract( $args );
            $option_name = 'nl_theme_options';
            $options = get_option( $option_name );
            switch ( $type ) {  
                case 'text':
                        $options[$id] = stripslashes( $options[$id] );
                        $options[$id] = esc_attr( $options[$id] );
                    echo "<input class='regular-text$class' type='text' id='$id' name='" . $option_name . "[$id]' value='$options[$id]'>";
                    echo ( $desc != '' ) ? "<br><span class='description'>$desc</span>" : "";
                break;

我需要初始化 $id 但不知道如何初始化.

I need to initialize $id but do not know how.

推荐答案

从这个问题和您之前的问题来看,您似乎正在从 nl_register_ga_code()$field_args> 到 nl_display_setting(),就像这样 nl_display_setting($field_args).(如果没有,那么您应该 - 根据您的评论,这就是您想要做的.)

From this question and your prior question, it looks like you are passing $field_args from nl_register_ga_code() to nl_display_setting(), like this nl_display_setting($field_args). (If not, then you should be -- based on your comments, that's what you're trying to do.)

所以,在nl_display_setting()中,不要引用$id$type$desc;分别参考 $args['id']$args['type']$args['desc'].

So, in nl_display_setting(), don't refer to $id, $type, or $desc; refer to $args['id'], $args['type'], and $args['desc'], respectively.

您不能将数组键当作独立变量来引用.所以,如果你有这个:

You cannot refer to an array key as though it were a standalone variable. So, if you have this:

$foo = array('bar' => 1234);

你不能用$bar得到1234,但是你可以用$foo['bar']得到它.

You can't use $bar to get 1234, but you can use $foo['bar'] to get it.

这篇关于如何从另一个函数初始化 $id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆