我制作了两个自定义类:
我正在对一系列内容进行自定义计算,我想将其保存在post\\u元字段的数组中。
This is how I save it
$obj1 = new FooBar();
$obj2 = new BarSchizzle();
$obj3 = new FooBar();
$arr = [
\'a_key\' => $obj1,
\'another_key\' => $obj2,
\'a_third_key\' => $obj3,
];
update_post_meta( $post_ID, \'my_custom_field\', $arr );
// I also tried this, with same result
// update_post_meta( $post_ID, \'my_custom_field\', serialize( $arr ) );
This is how I retrieve the array
$initial_arr = get_post_meta( $post_ID, \'my_custom_field\', true );
$arr = unserialize( $initial_arr ); // And this is where the error occurs
我收到错误信息:unserialize() [function.unserialize]: Error at offset ...
(您可以阅读更多信息here).我的理论是,WordPress没有加载(需要)我的自定义类,当它得到(并且未序列化)时。
我只需要简单地将这些类插入functions.php
-文件,在顶部,如下所示:
require_once( __DIR__ . \'/classes/FooBar.php\' );
require_once( __DIR__ . \'/classes/BarSchizzle.php\' );
。。。不在任何钩子或任何东西里面。感觉有点邋遢/糟糕,但到目前为止一直工作得很好。<小时/>
So this leaves me with two questions:
<我是否需要将我对自定义类的需求放入钩子中,例如init
还是什么$arr, 是否包含自定义对象