我目前正在制作一个Wordpress插件,我几乎可以使用它。它是一个自定义帖子类型,包含默认值,列和自定义字段(高级自定义字段)。
function newplugin_install() {
$new_post = array(
'post_title' => 'One Once Un Single',
'post_content' => '(FIRST). Lorem ipsum dolor set elit...',
'post_status' => 'publish',
'post_type' => 'exhibitor'
// INSERT default.png here somehow
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
}
我想根据特色图片或基于ACF图像类型添加默认图像。我不知道从哪里开始。
非常感谢,
答案 0 :(得分:0)
1-首先,如果您需要插入帖子
注意:如果你知道帖子ID并且你不需要插入新的,你可以跳过第二步
$new_post = array(
'post_title' => $title,
'post_content' => $outputValue,
'post_category' => array($_POST['cat']), // Usable for custom taxonomies too
'tags_input' => $tags_keywords,
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post' //'post',page' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
2-秒插入图像
//代码:如果你想从网址插入图片
if(isset($_POST['the_img_link']) && $_POST['the_img_link'] != ''){
$image_url = $_POST['the_img_link'];
basename($image_url);
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if(wp_mkdir_p($upload_dir['path'])){
$file = $upload_dir['path'] . '/' . $filename;
}else{
$file = $upload_dir['basedir'] . '/' . $filename;
}
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $pid );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $pid, $attach_id );
}else{
// $other_attach_id is any previous added attached image
// you want it to be a default image
set_post_thumbnail( $pid, $other_attach_id )
}
//代码:如果你想插入你的电脑的图像
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$attach_id = wp_insert_attachment( $attachment, $file, $pid );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $pid, $attach_id );
}
}else{
// $other_attach_id is any previous added attached image
// you want it to be a default image
set_post_thumbnail( $pid, $other_attach_id )
}