我在运行php版本5.2.17的服务器上发现一个php错误,该版本不支持匿名函数。我以前从未听说过这一点(对php来说真的很陌生),但我发现只有php 5.3。x支持以下代码:
add_action(\'init\', function() use($name, $args) {
有没有办法解决这个问题?它在5.3.10版上运行良好,但我不得不使用一个不使用php 5.3的旧主机。。。
这是创建自定义帖子的完整代码:
/* 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\')
));
最合适的回答,由SO网友:Eugene Manuilov 整理而成
您必须在没有闭包的情况下重写代码。可能是这样的:
function wpse54191_plugin_init() {
add_post_type(\'Netherlands\', array(
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'comments\')
));
}
add_action(\'init\', \'wpse54191_plugin_init\');
/* Add Post Type */
function add_post_type($name, $args = array() ) {
if ( !isset($name) ) return;
$name = strtolower(str_replace(\' \', \'_\', $name));
$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);
}