我正在尝试将图片上传到名为' event_items'的自定义帖子类型。 我在那里有一个名为“event-image'”的自定义字段。我非常喜欢它去那里,但如果它使用自定义的帖子类型特色图像,那就没问题了。
无法确定如何或在何处添加它。已尝试过示例,但尚未使用过。谢谢。
<?php
if(isset($_POST['submit'])){
$title=$_POST['title'];
$body=$_POST['body'];
$news_category=$_POST['region_names'];
$state=$_POST['state'];
// Create post object
$post = array(
'post_title' => $title,
'post_content' => $body,
'post_type' =>'event_items',
'post_status' => 'pending',
'post_author' => 1
);
// Insert the post into the database ref.https://codex.wordpress.org/Function_Reference/wp_insert_post
$post_id = wp_insert_post( $post, $wp_error='' );
if($post_id!=0){
///upload image ref.
https://codex.wordpress.org/Function_Reference/add_post_meta
///ref. https://codex.wordpress.org/Function_Reference/wp_handle_upload
if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['image'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
// echo "File is valid, and was successfully uploaded.\n";
var_dump( $movefile);
} else {
echo "Possible file upload attack!\n";
}
add_post_meta($post_id, 'event_photo', $movefile['url'] );
///////////////////////////////////////////////////////////
//insert taxonomy terms
//ref. https://codex.wordpress.org/Function_Reference/wp_set_object_terms
////for taxonomy 'news-category'//////////////////////////////
$cat_ids = array($news_category);
$cat_ids = array_map( 'intval', $cat_ids );
$cat_ids = array_unique( $cat_ids );
$term_taxonomy_ids = wp_set_object_terms($post_id, $cat_ids, 'region_names' );
/////////////End for taxonomy 'news-category'//////////////////////////////
////for taxonomy 'st'//////////////////////////////
$cat_ids2 = array($state);
$cat_ids2 = array_map( 'intval', $cat_ids2 );
$cat_ids2 = array_unique( $cat_ids2 );
$term_taxonomy_ids2 = wp_set_object_terms($post_id, $cat_ids2, 'st' );
/////////////End for taxonomy 'news-category'//////////////////////////////
echo "New successfully added";
}
}
?>
<h1>Add News</h1>
<form action="" method="post" enctype="multipart/form-data">
<table width="295" height="346" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="title" /></td>
</tr>
<tr>
<td>Promo Excerpt</td>
<td><input type="text" name="promo-title" /></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="body" ></textarea></td>
</tr>
<tr>
<td>Upload Image </td>
<td><input type="file" name="image" /></td>
</tr>
<tr>
<td>State</td>
<td><?php
//ref. https://codex.wordpress.org/Function_Reference/get_terms
$terms2 = get_terms("st",'order_by=count&hide_empty=0');
if ( !empty( $terms2 ) && !is_wp_error( $terms2 ) ){
echo "<select name='state'>";
echo "<option selected='selected'> Select </option>";
foreach ( $terms2 as $term2 ) {
echo "<option value='".$term2->term_id."'>" . $term2->name . "</option>";
}
echo "</select>";
}
?></td>
</tr>
<tr>
<td>Category </td>
<td>
<?php
//ref. https://codex.wordpress.org/Function_Reference/get_terms
$terms = get_terms("region_names",'order_by=count&hide_empty=0');
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
echo "<select name='region_names'>";
echo "<option selected='selected'> Select </option>";
foreach ( $terms as $term ) {
echo "<option value='".$term->term_id."'>" . $term->name . "</option>";
}
echo "</select>";
}
?>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"/></td>
</tr>
</table>
答案 0 :(得分:0)
如果我做对了,你想支持自定义帖子类型中的精选图片部分,所以你要做的只是将这一行添加到你的functions.php中:
add_post_type_support( 'your Custom Post Type Name', 'thumbnail' );
它会添加精选图片功能,然后您可以在循环“the_post_thumbnail()
”或循环外使用get_the_post_thumbnail($post_id);
获取缩略图。