Time for action – adding featured images to our theme

For our theme to display featured images, we need to follow two steps – adding a new function for featured image support to functions.php, and then add some code to display the featured images to our template files.

  1. Open your theme's functions.php file.
  2. Above the closing ?> PHP tag, add the following code:
    add_theme_support( 'post-thumbnails' );
  3. Save your functions.php file.
  4. Open your theme's index.php file.
  5. In the loop, insert a line break between the opening of <div class="entry-content"> and above the PHP call for the content itself.
  6. Add the following code:
    <?php if ( has_post_thumbnail() ) {
      the_post_thumbnail('large'),
    } ?>
  7. Save the file.
  8. Now open your theme's page.php file and repeat step 2 to step 4.

What just happened?

We added the code needed to display featured images for each post and page. Let's look at that code in detail:

  • First, it checks if the post or page has a thumbnail (or featured image) assigned to it
  • If so, it displays the image using the large size

Now let's add some featured images to our site and see how they look.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.5.86