这篇文章已于2015年11月30日编辑,我自己已经回复了。正确的脚本位于这篇文章的底部。谢谢,祝你好运!
问题:
要解释一下,我正在使用MAMP工作localhost,我正在使用WP ALL IMPORT插件将数据输入wordpress ..我有一个名为BOOK YOUR TRAVEL的主题。它有一个自定义帖子和一个名为“accommodation_images”的特定自定义字段。它将图像ID存储在序列化的整个数组中。很长一段时间,我没有意识到我不必将自己序列化。我找到了类似用途的脚本,并尝试过使用它们。
我设法将所有图片导入并附加插件,然后导入自定义字段“accommodation_images”更难。一旦它到达foreach我就得到空阵。当我评论foreach循环时,只有当我使用以下行
时才会得到一个条目$atts[] = array("image" =>$attid);
单一条目显然是最后一条,因为它循环通过所有..
但它的格式正确:
a:1:{i:0;a:1:{s:5:"image";s:5:"55079";}}
当数据超过1时,这就是条目的外观。
a:3:{i:0;a:1:{s:5:"image";s:5:"16487";}i:1;a:1:{s:5:"image";s:5:"77726";}i:2;a:1:{s:5:"image";s:5:"77722";}}
unserialize将此数组显示为
Array (
[0] => Array
(
[image] => 16487
)
[1] => Array
(
[image] => 77726
)
[2] => Array
(
[image] => 77722
)
)
以下是我一直在使用的代码
//This hook is called after WP All Import creates/updates a post meta.
//$pid – the ID of the post/page/Custom Post Type that was just created.
//$attid – the ID of the attachment
//$image_filepath – the full path to the file: C:\path\to\wordpress\wp-content\uploads\2010\05\filename.png
add_action('pmxi_gallery_image', 'update_images_meta', 10, 3);
function update_images_meta( $pid, $attid, $image_filepath ) {
//$attachment = get_post($attid);
// do something with $attachment image
// Get all the image attachments for the post
$param = array(
'post_parent' => $pid,
'post_type' => 'attachment',
'post_mime_type' => 'image'
);
$attachments = get_posts($param);
// Initialize the array
$atts = array();
// Fill the array with attachment ID's
foreach ($attachments as $attachment) {
//$atts[] = $attachment->ID;
array_push($atts, array("image" =>$attid));
//$atts[] = array("image" =>$attid);
}
//$serialized = serialize($atts);
//echo $serialized;
// unhook this function so it doesn't loop infinitely
//remove_action( 'save_post', 'update_images_meta', 10 );
// Update the post's meta field with the attachment arrays
update_post_meta($pid, 'accommodation_images', $atts);
// renable save post action
//add_action( 'save_post', 'update_images_meta', 10 );
}
?>
我需要修复循环以便将其余图像添加到数组中。
=============================================== ===
答案 0 :(得分:1)
(更新)花了很多时间学习php我找到了解决方案.....
答案:答案:2015年11月30日 - 我找到了一种方法来做到这一点,我希望它可以帮助某人。您必须确保您正在测试的图像已生成附件,否则显然不起作用。我强制它并通过设置插件WP ALL IMPORT重命名图像来解决这个问题,因为它上传它们的方式与以前不同,所以它创建了新的附件。祝你好运!<?php
//This hook is called after WP All Import creates/updates a post attachment file(s).
add_action('pmxi_gallery_image', 'update_images_meta', 10, 3);
function update_images_meta($pid, $attid, $image_filepath)
{
$attachments = get_children(array(
'post_parent' => $pid,
'post_type' => 'attachment',
'post_status' => 'inherit',
'order' => 'ASC',
'post_mime_type' => 'image'
));
//print_r($result);
$atts = array();
// Fill the array with attachment ID's
foreach ($attachments as $attachment) {
array_push($atts, array(
"image" => "$attachment->ID"
));
}
//echo '<pre>'.print_r($attachments,true).'</pre>'; //for testing
//echo '<pre>'.print_r($attachment,true).'</pre>'; //for testing
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'update_images_meta', 10);
// Update the post's meta field with the attachment arrays
update_post_meta($pid, 'accommodation_images', $atts);
// renable save post action
add_action('save_post', 'update_images_meta', 10);
}
?>
答案 1 :(得分:0)
这是Borisio脚本的更新版本。
<?php
//This hook is called after WP All Import creates/updates a post attachment file(s)
add_action('pmxi_gallery_image', 'add_images_ids_to_meta', 20, 3);
function add_images_ids_to_meta($post_id, $attid, $image_filepath)
{
$attachments = get_children(array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_status' => 'inherit',
'order' => 'ASC',
'post_mime_type' => 'image'
));
$atts = array();
foreach ($attachments as $attachment)
$atts[] = $attachment->ID;
// Update the post's meta field with the attachments ids
update_post_meta($post_id, 'gallery_images_ids', serialize($atts));
}
?>
这将图像ID保存在一个meta字段中。我必须删除那些“ add_action”和“ remove_action”,因为它们破坏了插件。