Thursday, November 28, 2013

Custom Post Type & Featured Image

You can add featured image in custom post type. Once you add the option, you will find a section in the edit screen from where you can add the image (refer to image 1).



How to add the option for featured image:

You can copy the following code to functions.php file in the theme folder:

<?php 
//Register Post Type
register_post_type( 
  'furniture',
  array(
     'labels' => array(
     'name' =>  'Furniture',  //this what will appear as admin menu item and custom post type admin screen
        'add_new' => 'Add new Furniture',
        'add_new_item' => 'Furniture',        
    ),
  'public' => true,
  'has_archive' => true,
  'rewrite' => array('slug' => 'furnitures'),
  'show_ui' => true, 
  'supports' => array('thumbnail','title','editor','comments', 'custom-fields')
  ));


register_taxonomy('type','furniture', array(
  'label' => 'Type',
  'hierarchical'=>true,
  'show_admin_column'=>true
 )
);
It is the 'thumbnail' that does the magic.

You can now add some furnitures (post) and add featured images. Now we will see how to develop a simple template file to view a single furniture.

Create a file named: single-furniture.php and add following contents to the file:

<h1><?php echo $post->post_title; ?> </h1>

<p style='font-size:1.2em;'><?php echo $post->post_content; ?> </p>

<?php
if ( has_post_thumbnail() ) {
 the_post_thumbnail('medium');
} 
?>
Following is the output: