3

将我的 Wordpress 文件上传到运行 php 版本 5.2.17 的服务器后,我收到了一个意外的 T_FUNCTION php 错误。

该主题在 localhost(使用 MAMP)上运行良好,并且在我自己的运行 php 版本 5.3.10 的服务器上也没有错误。

可能有什么问题或可以采取什么措施来解决此错误?

这是导致错误的行:

add_action('init', function() use($name, $args) {   

整个functions.php文件看起来像这样:

<?php 

/* Add Post Type */
function add_post_type($name, $args = array() ) {   
    if ( !isset($name) ) return;

    $name = strtolower(str_replace(' ', '_', $name));

    add_action('init', function() use($name, $args) {   
        $args = array_merge(
            array(
                'label' => 'Members ' . ucwords($name) . '',
                'labels' => array('add_new_item' => "Add New $name"),
                'singular_name' => $name,
                'public' => true,
                'supports' => array('title', 'editor', 'comments'),
            ),
            $args
        );

        register_post_type( $name, $args);
    });
}


add_post_type('Netherlands', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));


add_post_type('Belgium', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Germany', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('France', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('United-Kingdom', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Ireland', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Spain', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Portugal', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Italy', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

我真的是 php 新手,只将它用于 Wordpress 主题。非常感谢任何帮助。

4

3 回答 3

7

低于 5.3 的 PHP 不能有匿名函数

重新编写您的代码,使其不涉及匿名函数,并且它应该可以在您的旧服务器上运行。

于 2012-06-04T18:30:23.847 回答
1

add_action()的第二个参数是回调类型。

在 5.3 之前,这通常是一个表示函数的字符串:

add_action('init', 'myFunction');

function myFunction() { echo 'init'; }

create_function在处理对象时,可以使用诸如和其他语法之类的替代方法。

从 5.3 开始,允许使用匿名函数:

add_action('init', function() { echo 'init'; });
于 2012-06-04T18:37:52.437 回答
-1

PHP 5.3.0 提供的匿名函数

于 2012-06-04T18:35:49.180 回答