Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you open a page or a post or a index page in WordPress what you're really doing is asking the database to return specific cells from its tables to you, in a specific layout. This is what's called a database query. This means that if you know how to manipulate the database queries, you can ask the database to return any information you want and place it anywhere. That's what we're going to do now. In my site, I've created a new page. It's called Front Page. And in this page all I have is three paragraphs of text.
What I want to do now is get this text to appear on the Front Page itself, above the index. So when people visit my site they'll first see that welcome message and then they scroll down and see the index. To get this to work, we're simply going to call the front-page content and display it into index.php template. Here I'm going to teach you a trick. When I start adding this type of content into my site, say a content slider or a welcome message or anything else that requires quite a bit of code, what I do is I place it in a separate file and then I just call that file into the template.
That way I know exactly where that content is if I need to edit it later. And it's also much easier to remove or add when I want to. So the first thing I'm going to do is create a new file. So I'll go to my code editor and save the file in my child theme. I'll call this file welcome because it's going to be the welcome message, and I'll make sure it's a PHP file. And this is the file I'm going to call that will then be displayed inside the index.php template.
So first off, I'm going to just write some dummy content so we can test that everything works. I'll start off with a simple comment to explain what this is, and then I'm going to populate it with some standard HTML content. Now you notice I'm pretty meticulous about how I write my HTML, and you might also wonder where I get all these ID names and everything from. This is just a habit. I like to really document what I do and I write everything out and I give every single elements an ID or a class so that I can easily target it with CSS later if I want to.
Now if you've done any kind of work with WordPress before, you may notice that I'm using the entry- content class for this div here. That's because I want the content I'm displaying at the top to have the same styling as to content that's in a regular post. And as you know, entry-content is generally the class that's applied to any post content, so that's why you see it here. So I am borrowing a style and applying it to my new content. Now I'm just going to put in some dummy content here so I can test to see that this works. And now I have all the content I need to make sure this works.
I have the welcome.php file and now I need to call it from the index.php template. So I'll go to my parent theme, find index.php, and copy it into my child theme, open it in my editor, and then I'm going to use a WordPress function to call in this welcome.php file. That function is called get_template_part. The variable is the name of the file that I want to call--in this case welcome-- and when I close this, save it, and reload my index page, you see that the content I just created-- this is the text that is displayed by the welcome.php file--appears here at the top of the index page.
That means index.php template is now calling the welcome.php file and displaying its content, so everything is working. The next thing I need to do is go back to welcome.php and change out this dummy content here with the real content from the Front Page page. So I'm going to dele the dummy content, and then I'm going to use a function from WordPress that calls in content from the database. The function is called get_page_by_ title, and like the name suggests, it finds the page based on the title of the page.
So I'm going to put in my PHP delimiters and then I'm going to make a variable, call it page--you can call it whatever you want--and I'll say that this variable should be filled with the content from the page I called, in this case get_page_by_title. And then I'll put in the title, Front Page. That's the title I set in WordPress when I saved the page. And now I'll have a variable that has all information that the database has, on the page called Front Page.
To see what that information is, I'm going to go down here and then put in a php function called print_r, and what print_r does is it prints out everything in a variable, regardless of what it is. So I'll just put in page, which is what we created up here, so I can see everything the database is returning. I'll save welcome.php and reload my Front Page. And now you see I get the complete return from the database. This is all information that database has on the Front Page page, and it returns it all to me.
Looking at this, I can see that all I really need is this. This is the actual content from the page, and it's contained inside an array that has the tag post_content. So what I want to do is instead of returning all of it, I just want to see the content that's under post_content. I'll go back to welcome.php and change this to echo $page, because that's the variable, and then I only want post_content. I'll save that, reload the Front Page again, and now you see I only have the contents inside post_content.
But if I open the Front Page page, you'll notice that here I have three paragraphs, but when it's returned on my index page, it's all mushed together. That's because WordPress is not treating this as real content yet. It's just returning all the content as it is. So the last thing I need to do is apply a filter to the post_content. I'm simply going to tell WordPress to treat the post_content as the content of a post. And I can do that using a function called apply_filters.
So I'll say apply_filters. Then I'll tell WordPress what type of content this is. This is the content of a page. Then I just need to close my parenthesis, save welcome.php again, reload my index page, and now you see that the content is formatted in the exact same way it is in the page itself. Now that I have my Front Page content displayed on the Front Page, I'm going to hide the Front Page button in my menu.
So I'll go to the backend, to menus, and apply a menu that doesn't have a Front Page. I've already created one here to the main menu. Save it, reload, and now we have a seamless experience. You have a main menu here, you have the Welcome message, and then you have the index underneath. It is outside the automatic template queries generated, the real magic of WordPress happens. Once you wrap your head around the fact that you can call any content at any time from anywhere, you'll realize that can do pretty much anything you want, and the only limitation is your own imagination.
Now we have some basic content, but that's just the beginning.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.