wordpress featured image code example

Example 1: get featured image url in wordpress

if ( has_post_thumbnail()) {
   $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
   echo '<a href="' . $large_image_url[0] . '">';
   the_post_thumbnail('thumbnail');
   echo '</a>';
}

Example 2: wordpress featured image show

<?php echo the_post_thumbnail(); ?>

Example 3: get featured image id wordpress

get_post_thumbnail_id($post)

Example 4: show featured image in post wordpress

<?php if(has_post_thumbnail()){
  the_post_thumnbail('cstm_name', array( 'class' => 'class1 class2' ))
} else { ?>
  <img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap" class="img-responsive">
<?php } ?>

Example 5: add featured image

function fn_add_featured(){
   add_theme_support( 'post-thumbnails' );
   add_image_size( 'post-imge', 730, 380 );
}

add_action('after_setup_theme','fn_add_featured');

Tags:

Php Example