如何声明可以多次使用的数组?

时间:2016-08-24 11:09:58

标签: php wordpress

我已经 一些新的PHP / Wordpress,并且正在努力尝试让某些东西发挥作用。

基本上,我尝试在主题自定义程序中创建一个部分,让我从Google字体数组中进行选择。

我有这样的数组:

$wp_customize->add_control(
'site_title',
array(
    'type' => 'select',
    'label' => 'Site Title:',
    'section' => 'typography_section_one',
    'choices' => array("ABeeZee" => "ABeeZee",
          "Abel" => "Abel",
          "Abril Fatface" => "Abril+Fatface",
          "Aclonica" => "Aclonica",
          "Acme" => "Acme",
          "Actor" => "Actor",
          "Adamina" => "Adamina",
          etc...
    )
   )
);
}

add_action( 'customize_register', 'type_customizer' );

所以,一切正常。没问题。我可以看到我的部分,我可以改变我的字体,这一切都很棒。

但是我想要一些这样的实例,因此用户可以更改其网站的多个部分的字体。事情是,我不想每次创建新控件时都要保留复制和粘贴这大量字体名称。

所以,这是我的问题。无论如何我可以"全球化"这个数组可以多次使用吗?

提前感谢一大堆!

2 个答案:

答案 0 :(得分:1)

一种简单的方法是将数组放入其自己的文件中。

<强> my_fonts.php

<?php

$fonts = array(
    "Abel" => "Abel",
    "Abril Fatface" => "Abril+Fatface",
    "Aclonica" => "Aclonica",
    "Acme" => "Acme",
    "Actor" => "Actor",
    "Adamina" => "Adamina",
);

return $fonts;

然后按如下方式使用:

$wp_customize->add_control(
    'site_title',
    array(
        'type' => 'select',
        'label' => 'Site Title:',
        'section' => 'typography_section_one',
        'choices' => include('my_fonts.php'),
    )
);

如果您使用的是php 7,您还可以将数组定义为常量,如下所示:

define('MY_FONTS', array(
    "Abel" => "Abel",
    "Abril Fatface" => "Abril+Fatface",
    "Aclonica" => "Aclonica",
    "Acme" => "Acme",
    "Actor" => "Actor",
    "Adamina" => "Adamina",
));

答案 1 :(得分:1)

有两种方法可以做到这一点:

使用全局关键字:

$fonts =  array("ABeeZee" => "ABeeZee",
      "Abel" => "Abel",
      "Abril Fatface" => "Abril+Fatface",
      "Aclonica" => "Aclonica",
      "Acme" => "Acme",
      "Actor" => "Actor",
      "Adamina" => "Adamina",
      etc...
);

function customize_register() {
    global $fonts;

    $wp_customize->add_control(
        'site_title',
        array(
            'type' => 'select',
            'label' => 'Site Title:',
            'section' => 'typography_section_one',
            'choices' => $fonts
        )
    );
}
add_action( 'customize_register', 'type_customizer' );

Thw全局关键字会将变量$array拉入函数范围,使其可用。这是最容易和最不可取的可能性。

最好的方法是将所有代码包装到一个类中:

class foobar {

    var $fonts = array(
        "ABeeZee" => "ABeeZee",
        "Abel" => "Abel",
        "Abril Fatface" => "Abril+Fatface",
        "Aclonica" => "Aclonica",
        "Acme" => "Acme",
        "Actor" => "Actor",
        "Adamina" => "Adamina",
        etc...
    );

    function customize_register() {
        $wp_customize->add_control(
            'site_title',
            array(
                'type' => 'select',
                'label' => 'Site Title:',
                'section' => 'typography_section_one',
                'choices' => $this->fonts
            )
        );
    }

    function __construct() {
        add_action( 'customize_register', array( $this, 'type_customizer' ) );
    }

}

$foo = new foobar();

通过这种方式,您不必担心由于相同的变量名称而与其他插件/主题发生冲突,所有内容都干净地包装在一个类中。