所以,我有以下内容在wordpress帖子中添加图片:
<?php
global $current_user;
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter the content';
}
$tags = $_POST['post_tags'];
$custom_field_1 = $_POST['custom_1'];
$custom_field_2 = $_POST['custom_2'];
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => $_POST['cat'],
'tags_input' => $tags,
'post_status' => 'publish',
'post_type' => $_POST['post_type']
);
$pid = wp_insert_post($post);
add_post_meta($pid, 'rh_content', $custom_field_1, true);
add_post_meta($pid, 'rh_item', $custom_field_2, true);
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
}
}
wp_redirect( home_url() );
}
do_action('wp_insert_post', 'wp_insert_post');
?>
因此,它允许上传图像,可以通过get_the_post_thumbnail
显示。
<?php if ( has_post_thumbnail() ) { ?>
<div class="rhmi_thumb">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'rh_site') ?>
</div>
<?php } ?>
然而,即使没有上传图像(即没有缩略图),它仍会显示class="rhmi_thumb"
。所以,我猜测图片是否上传,帖子认为有缩略图。
在上传后的表单中应该进行哪些修改?
由于
答案 0 :(得分:1)
要将图片上传到页面/帖子,您必须使用wp_insert_attachment()。我已经从那里添加了wordpress网站的链接,你可以得到如何实现它的例子。谢谢!