在数组中分配变量

时间:2013-06-27 11:56:38

标签: php wordpress

我正在使用wordpress网站,我需要将一个函数的值赋给一个数组内的值,但它一直给我错误。在customFields数组中,我需要将get_option的值(' contact_email',#39; no one')插入到某个帮助文本中。在下面的代码段中,我需要将{CONTACT_EMAIL}替换为get_option值,但无法弄清楚。

...

    var $customFields = array(
        array(
            "name"          => "ContactPerson-name",
            "title"         => "Contact Name",
            "description"   => "Enter the first and last name of the page contact. If left blank, the site default of {CONTACT_PERSON} will be used.",
            "type"          => "textinput",
            "scope"         =>  array( "page" ),
            "capability"    => "edit_pages"
        ),
        array(
            "name"          => "ContactPerson-email",
            "title"         => "Contact Email",
            "description"   => "Enter the email address of the page contact. If left blank, the site default of {CONTACT_EMAIL} will be used.",
            "type"          => "textinput",
            "scope"         =>  array( "page" ),
            "capability"    => "edit_pages"
        ),

        array(
            "name"          => "ContactPerson-phone",
            "title"         => "Contact Phone (XXX) XXX-XXXX",
            "description"   => "Enter the phone number of the page contact. If left blank, the site default of {CONTACT_PHONE} will be used.",
            "type"          => "textinput",
            "scope"         =>  array( "page" ),
            "capability"    => "edit_pages"
        ),

        array(
            "name"          => "ContactPerson-address",
            "title"         => "Contact Room Number & Building",
            "description"   => "Enter the room number and building of the page contact. Click <a href=\"http://www.engin.umich.edu/buildingabbreviations\">here</a> for building abbreviations. If left blank, the site default of {CONTACT_ADDRESS} will be used.",
            "type"          => "textinput",
            "scope"         =>  array( "page" ),
            "capability"    => "edit_pages"
        ),

...

...

我试图关闭文本并连接函数,我已经尝试进行字符串替换,似乎没有任何工作。感谢任何人提供的任何帮助。

1 个答案:

答案 0 :(得分:2)

蛮力方法会起作用......

$customFields = array(
    array(
        "name"          => "ContactPerson-name",
        "title"         => "Contact Name",
        "description"   => "... of {CONTACT_PERSON} will be used...",
        "type"          => "textinput",
        "scope"         =>  array( "page" ),
        "capability"    => "edit_pages"
    )
);

$contact_person = get_option('contact_person');

foreach($customFields as &$field)
{
    $field['description'] = str_replace("{CONTACT_PERSON}", $contact_person, $field['description']);
}

unset($field);