Reputation: 5591
So, I have the following to add an image in the wordpress post:
<?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');
?>
So, it allows an image to be uploaded which can be shown by 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 } ?>
However, even without the image uploaded (ie. there is no thumbnail), it still shows class="rhmi_thumb"
. So, I am guessing that the whether an image is uploaded or not, the post thinks that there is a thumbnail.
What modification should be made in the post upload form?
Thanks
Upvotes: 0
Views: 50
Reputation: 2928
For uploading image into page/post you have to use wp_insert_attachment(). I have added the link of wordpress site from there you can get the example that how you can implement that. Thanks!
Upvotes: 1