我正在使用插件WP All Import来导入一个特殊的帖子类型。该插件包括一个动作挂钩pmxi\\u gallery\\u图像,该图像在导入后运行,将附件图像保存到post gallery。
我已将此操作包含在函数中的函数中。“我的孩子”主题中的php文件:
//After WP All Import runs this will place the attached photos in the gallery images field by post id
add_action(\'pmxi_gallery_image\', \'update_images_meta\', 10, 3);
function update_images_meta( $pid, $attid, $image_filepath ) {
// Get all the image attachments for the post
$param = array(
\'post_parent\' => $pid,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\'
);
$attachments = get_children( $param );
// Initialize the array
$atts = array();
// Fill the array with attachment ID\'s
foreach($attachments as $attachment) {
$atts[] = $attachment->ID;
}
// Disable this hook which was overwrinting our changes
// remove_action( \'save_post\', \'flexible_save_details\', 10 );
// Update the post\'s meta field with the attachment arrays
update_post_meta( $pid, \'images\', $atts );
}
这将输出一组图像帖子id:
a:4:{i:0;i:38985;i:1;i:38986;i:2;i:38987;i:3;i:38983;}
我需要在id之前包含一个字符串长度,id用引号封装,因此主题使用的库的输出如下所示:
a:4:{i:0;s:4:"7613";i:1;s:4:"7615";i:2;s:4:"7616";i:3;s:4:"7618";}
最合适的回答,由SO网友:s_ha_dum 整理而成
您的数据正在正确序列化。您的代码保存一个整数数组。整数不是字符串,因此不会保存字符串长度。运行以下代码,应该可以说明这一点:
$str = \'a:4:{i:0;s:4:"7613";i:1;s:4:"7615";i:2;s:4:"7616";i:3;s:4:"7618";}\';
$str = maybe_unserialize($str);
var_dump($str);
echo \'<br/>\';
var_dump(maybe_serialize($str));
echo \'<br/>\';
$str = array(
1234,4567,8910,1112
);
var_dump(maybe_serialize($str));
echo \'<br/>\';
$str = array(
\'1234\',\'4567\',\'8910\',\'1112\'
);
var_dump(maybe_serialize($str));
要获得所需的结果,请保存字符串而不是整数:
// Fill the array with attachment ID\'s
foreach($attachments as $attachment) {
$atts[] = "$attachment->ID";
}