Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Create a child theme based on an existing parent theme in WordPress and change the functionality, presentation, or styling of your website. In this course, author Morten Rand-Hendriksen shows how to use the default WordPress theme, Twenty Twelve, as a basis for a new child theme and add custom menus; new headers, sidebars, and footers; and index pages with widgets and pagination to your site. The course also demonstrates how to add a responsive welcome message to your front page using PHP and jQuery, and how to edit the many templates found in a WordPress theme. Morten explains how to perform these changes using any code editor, the developer tools in the Chrome browser, and WordPress.
Let's look at an example of how you can override an existing function found in twentytwelve and replace it with a whole new function in your child theme. Though they're standard in WordPress themes, I don't particularly like the Older posts and the Newer posts links at the bottom of index pages. You can see it down here at the bottom. It says Older posts. When you click on it you go to page two and then you get either Older posts or Newer posts. I much prefer proper pagination lists where I can see how many total pages there are, and I can navigate in a more advanced way.
Now I want to replace the existing function that creates this Older posts and Newer posts link with a proper navigation menu instead. twentytwelve comes with a function called twentytwelve_content_nav. If we go into functions.php and search for it--I'll just search for a content_nav; here it is--you'll see that the function down here is wrapped in a conditional statement that tests whether or not the child theme already has this function defined.
So all we need to do is create a new function in our child theme functions.php file, and that one will automatically override the function in the parent theme. So I'm going to copy the start of this function here, and then I'm going to go and create a new file, and I'll save it under my child theme folder as functions.php. Make sure it's a PHP file.
And here I'm going to start it with a php delimiter and end it with a php delimiter and then just put in a comment to explaining what it is, and then I can put in my new function here. Now, I know I don't need this variable nav_id anymore because I've done some experimentation before. All I'm going to do is define the function itself and then end the function, and if I just save this file now and reload my page in my browser, you'll see that I lost my navigation completely.
That's because my new function here is taking over and that function isn't doing anything. The next step is to find a function that will work the way I want it to, and for that I'm going to go to the all-powerful Internet. The great thing about open source is that a lot of people come up with good ideas and then they publish them online. I do that myself, and most people who work with WordPress will publish code snippets that you can just take and augment and use for your own purposes. For pagination I found this great article called How to build a WordPress Post Pagination without plugin at kriesi.at.
This is a solution I've used many times, though I've augmented it quite a bit, and this is the solution we're going to use in the theme now. If you read this article, you'll see the explanation of how the function works with code examples and also how the style works, and you can see how this all fits together so you get this nice navigation menu. What I have done for this course is I've supplied you with an alternate version of this code specifically for the twentytwelve child theme that you can copy out of the code snippets file and paste into your functions.php file.
So we'll go to the codesnippets file, scroll down to this chapter number, 5.3, and here you'll find the function. So I will copy it all out, go into my functions.php file, in my child theme, and I'll just paste all that code in. You see here it's quite a lot of code, and I've also added some extra comments here, along with a link directly to that original article, and I've made some small changes to it to make it work the way I want it to, but you're free to make changes to it yourself if you want to as well.
I'll save this and reload my page here, and you'll see now we have navigation here at the bottom. So we can jump between pages if we want to. Scroll down. And what's cool about this particular pagination solution is that if I go in and change my settings here so that it appears like we have a lot of content--I'll do that by going into Settings > Reading, and setting it so that we only see one post per index page-- you'll see that at the bottom of the page not only do we get the numbers, but we also get a Next button so you can jump to the next one, and you get this button at the end that takes you to the very end of the list.
But this doesn't look very good; I need some style to augment it so that it looks nice on my site. So I'll go back to my code snippets, scroll down, and here you'll find pagination styles that I've created specifically for this theme so that it works. We can copy that out, go up to the style sheet in child of twentytwelve, open the style sheet in your child theme, and then paste in the new styles. I'll paste them in here at the bottom. We see there's quite a few styles here that will style the different elements. Save that, reload your page again, and now you have a nice pagination bar here at the bottom that works exactly the way you would expect pagination to work.
Now you can see why using a properly built theme as your parent theme saves you a lot of time and headache. Because the twentytwelve_content_nav function was properly wrapped in a conditional statement, we have an easy time overriding it and adding in new functionality. As a result, we've now added a great new functional pagination system to all our index pages with minimal effort.
Find answers to the most frequently asked questions about WordPress 3: Building Child Themes.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
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.