如何使用自定义帖子类型和自定义分类法为帖子添加精选图片?

时间:2015-03-16 18:16:12

标签: php wordpress

我有一个网站,允许用户将照片添加到自己的帐户 用户可以通过在html表单上上传自定义帖子并使用set_post_thumbnail设置特色图片,成功上传照片。

我已经添加了自定义分类法,允许用户将他们上传的每张照片分配到相册。但是,这已经阻止了图像以某种方式上传。该帖子正在上传到指定的相册中,但没有图像。图像不会出现在WP后端的媒体部分中。

if ($_POST == true){

		$post = array(
			'comment_status' => 'open',
			'ping_status' => 'closed',
			'post_date' => date('Y-m-d H:i:s'),
			'post_status' => 'publish' ,
			'post_title' => $_POST['Title'],
			'post_type' => 'music_review',
			'post_content' => $_POST['Comment']
			
			//inserts new post into 'uploads' database
			);
	$new_post = wp_insert_post( $post );
	require_once( ABSPATH . 'wp-admin/includes/image.php' );
	require_once( ABSPATH . 'wp-admin/includes/file.php' );
	require_once( ABSPATH . 'wp-admin/includes/media.php' );
		
		
		
		$terms = $_POST['album']; //takes name from 'album' in html
		
		//if checkbox is ticked
		if (isset($_POST['album'])) { 		//Username ='". $_GET['user']."'";
				//echo "YES"; 
				
				$assign_to_album = wp_set_object_terms( $new_post, $terms, 'Albums'); 
				
				
			}
			
			
		$attachment_id = media_handle_upload( 'image', $new_post );
		//new dBug($_FILES['image']);
		set_post_thumbnail($new_post, $attachment_id);
		//inserts the image to the post 
		
		
		add_post_meta($new_post, 'Username', $user_data['Username']);
		//adds custom field to the post, assigns username of current
		//logged in user to the custom field
		
		//$assign_to_album = wp_set_object_terms( $new_post, $link[0] . "-" . $link[1], 'Albums'); 
		echo "Your upload has been successful!<br>";
		
		
		}
       ?>

	
    	<div id="content">

    	<form name="uploadingImage" enctype="multipart/form-data" action="<?php echo $_SERVER["REQUEST_URI"]?>" method="post">
         <br>Enter a title for your upload: <input type="text" name="Title"/>
    <br>
    <input type="file" name="image"/>
    Select an album for your upload.  You can choose more than one album.
    <?php  


    // puts albums as checkboxes 
    $albums = get_terms( 'Albums', array('orderby'=> 'count','hide_empty' => 0,) );								
    				foreach ($albums as $album)
    				{
    							// Casts each album(object) to an array
    							$albumarray = (array) $album;							
    							//sets $albumname as only the album name from each object
    							$albumname = ($albumarray['name']);							
    							//converts the album name variables into an array
    							$albumnames = array($albumname);										
    							{
    								foreach ($albumnames as $link)
    									{									
    										 // splits each album name into an array, with each item divided by a '-'
    										$link = explode("-", $link);										
    										if ($user_data['Username'] == $link[0])
    										{    //new dBug($link);	
    										 ?>
    											<input type = "checkbox" name='album' value="<?php echo $link[0] . "-" . $link[1]?>" checked><?php echo $link[1]?>
    											<br>																	
    											<?php																		
    										} 
    									}		
    							}
    				}?>
    											<input type="submit" value="Submit!" />
    											</form>

    											<br>


    	

    	<?php the_content() ?>


    	<?php get_footer(); ?

1 个答案:

答案 0 :(得分:0)

array('supports' => 'thumbnail')

将此添加到您的CPT声明中。