Now, if we go to the form shown here to add a post, and you look on the right-hand side, you don't see a Featured Image box, which you may have seen in other WordPress themes or on other WordPress websites. The reason for that is we didn't include that functionality:
- Let's go into our themes folder, and we'll create a file and save it as functions.php. Now, this is where we can put a custom function; this is where we can hook into certain parts of the code, register widgets, and do all that—basically, anything dynamic.
- Here, we'll create a setup function. To do this, we will add the following code block:
<?php
function simple_theme_setup(){
// Featured Image Support
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'simple_theme_setup');
Here, we have added a comment, Featured Image Support, followed by a function called add_theme_support. Now this function isn't going to run if we leave it just like this. So, we have used add_action and certain hooks that we can hook into and run this function. The one that we want is called after_setup_theme. Then, the second parameter will be the name of the function. We will save it and then run it.
- Let's go back and reload this page:
Now you'll see down at the bottom that we have the Featured Image box. If we click on that, we can go ahead and upload files, as shown here:
- Now, I'll choose an image and save it so that you can see it's inserted, and we can then click on Update:
Now, if we go to the frontend and reload, we'll not see it yet, because we didn't add it to our index.php file.
- Let's go to the index.php file. You don't have to show the image in the index. You can just show it in single page if you want, but usually you want it to be shown on both pages. So, in index.php, let's just add the image right below meta, as shown here:
<div class="meta">
Created By <?php the_author(); ?> on <?php the_time(
'F j, Y g:i a'); ?>
</div>
<?php if(has_post_thumbnail()) : ?>
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
At first, it's not going to look too great, but we'll fix that later on. In the preceding code, we have first checked to see if there's an image using an if statement. If there's a thumbnail, then we'll create a <div> tag. We'll give it a class of post-thumbnail and then add php the_post_thumbnail, as shown in the preceding code block.
- Let's save it, go back, and reload. You will get the following result:
Now, it looks a little out of the way because it's not reaching 100% across the page, but we're actually going to put a sidebar in a little bit, when we get to the widgets, so that it looks a lot better. However, I am going to add a width of 100% in the CSS, where we have this post, this class="post-thumbnail".
- Let's go into our style sheet and add the following code block:
.post-thumbnail img{
width:100%;
height:auto;
}
For the image, we want the width to be 100% and we can keep height:auto.
- When we reload, we can see that it reaches across the whole page:
I know that's really big, but like I said, we'll get a sidebar in there, and it'll shrink that a little bit.
- Now we also want the image on the single post page. So we'll copy the code from index.php and paste it in the single.php file, right under meta:
<div class="meta">
Created By <?php the_author(); ?> on <?php the_time(
'F j, Y g:i a'); ?>
</div>
<?php if(has_post_thumbnail()) : ?>
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
Now we'll get the same image on the single page as well.
As you can see, in the posts on the index page, it shows the whole content, but we want just a piece of the content to show.
- To do that, let's go to the index.php file, and instead of saying the_content, we can actually say the_excerpt, as shown in the following code:
<?php the_excerpt(); ?>
What this will do is, it will cut the content to around 55 words. You can actually customize that and make it as long or as short as you want, and that's actually really easy.
- Go to the functions.php file and create a custom function, as shown here:
<?php
function simple_theme_setup()>{
// Featured Image Support
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'simple_theme_setup');
// Excerpt Length
function set_excerpt_length(){
return 25;
}
add_filter('excerpt_length', 'set_excerpt_length');
We'll just say set_excerpt_length, and all we need to do here is just return a number, so let's say we wanted it to be 25. Then, what we can do is, instead of add_action, we'll say add_filter, because add_action is usually used to add something or create something; add_filter is usually used to change something. The filter we want to hook into here is called excerpt_length. Then, we just want to put in the name of our function, which is set_excerpt_length.
- If we go back and reload, we can see that it's only 25 words long. If you want it a little longer, let's say return 60. I just wanted to show you that it's possible.
So we have our index page set up, and we have our single post page set up, apart from the comments, which we'll do a little later. However, in the next section, we will see how we can create a menu and start dealing with pages as well as posts.