# WordPress Featured Image dan Attachment Object
> Enable featured image di template WordPress dan memanfaatkan attachment object di dalam WordPress template
**Untested - Kalau ada error tolong direvisi**
## Enable fitur Featured Image di Themes
Aktifkan melalui `functions.php` template, fitur featured image di dalam WordPress dikenal dengan `post-thumbnails`
```php
<?php
/**
* Theme's functions file
*/
// Enable featured image
add_theme_support( 'post-thumbnails' );
```
## Hook ke Featured Image di dalam WordPress loop
Post thumbnail dari sebuah post dapat diakses melalui template tag `get_the_post_thumbnail` dan kita bisa memanfaatkan data yang ada di dalam object attachment melalui ` get_post_thumbnail_id`.
```php
<?php
// tampilkan featured image ke dalam template tag
// terekspos ketika berada di dalam loop
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
```
Sementara untuk mengambil value tanpa menampilkan - contoh implementasi untuk memberikan style tambahan.
```php
<?php
// tampilkan featured image ke dalam template tag
// terekspos ketika berada di dalam loop
// Syntax get_the_post_thumbnail( int|WP_Post $post = null, string|array $size = 'post-thumbnail', string|array $attr = '' )
$post_id = get_the_ID();
if ( has_post_thumbnail() ) {
echo get_the_post_thumbnail( $post_id, 'thumbnail', array( 'class' => 'alignleft' ) );
}
```
## Mendapatkan id dari featured image dan akses ke Attachment object
Manfaatkan `get_post_thumbnail_id`
```php
<?php
// ambil id dari featured image
// di dalam WordPress loop
$post_id = get_the_ID();
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
// ambil metadata
// syntax wp_get_attachment_metadata( $attachment_id, $unfiltered );
$metadata = wp_get_attachment_metadata( $post_thumbnail_id , false );
```
`$metadata` merupakan array - contoh
```array
// output
Array
(
[width] => 2400
[height] => 1559
[file] => 2011/12/press_image.jpg
[sizes] => Array
(
[thumbnail] => Array
(
[file] => press_image-150x150.jpg
[width] => 150
[height] => 150
[mime-type] => image/jpeg
)
[medium] => Array
(
[file] => press_image-4-300x194.jpg
[width] => 300
[height] => 194
[mime-type] => image/jpeg
)
[large] => Array
(
[file] => press_image-1024x665.jpg
[width] => 1024
[height] => 665
[mime-type] => image/jpeg
)
[post-thumbnail] => Array
(
[file] => press_image-624x405.jpg
[width] => 624
[height] => 405
[mime-type] => image/jpeg
)
)
[image_meta] => Array
(
[aperture] => 5
[credit] =>
[camera] => Canon EOS-1Ds Mark III
[caption] =>
[created_timestamp] => 1323190643
[copyright] =>
[focal_length] => 35
[iso] => 800
[shutter_speed] => 0.016666666666667
[title] =>
)
)
```