Parent and child themes

This topic doesn't apply to the Open Source Online Magazine but it's worth explaining as it's a really useful feature of WordPress theme development.

Say you've been working with the Open Source Online Magazine theme or another one you've developed yourself or downloaded. You decide you want to build a second theme or site with a similar layout and design, or with the content structured in a similar way, but you don't want to start again from scratch.

You could just edit the theme you've already got to create a brand new one. But if your changes aren't huge, it may be better to build a child theme.

A child theme is a theme which is associated with another theme, called the parent theme. It takes most of its styling and content from the parent theme but has a few things which are different say, for example the archive listings are displayed differently. To do this, you would set up a child theme with its own stylesheet (all child themes need a stylesheet as we'll see shortly), and an archive.php file. It wouldn't need any other files—not even an index.php file, as it would take that from the parent theme.

To tell WordPress that a theme is the child of another theme, you have to add some text to the very top of its stylesheet—the style.css file:

/*
Theme Name:     My child theme
Theme URI:      http://example.com/
Description:    Child theme for the Open Source Magazine theme 
Author:         Your name here
Author URI:     http://example.com/about/
Template:       opensourcemagazine
Version:        1.0
*/

Some notes on this text:

  • It's commented out, browsers won't read it but WordPress will
  • The theme must have a name and a template, the other information is optional
  • The template is the parent theme – here you need to enter the directory your parent theme is in, in your themes directory (not its name)

Below this commented out code, you add a call to the parent theme's stylesheet:

@import url("../opensourcemagazine /style.css");

After this, you add any styling that's specific to the child theme. You don't need to repeat any styling that's in the parent theme.

When activating your theme in the WordPress theme's admin screen, you simply activate the child theme—WordPress does the rest for you.

Tip

Child themes are a great way to build up your own bank of great WordPress themes with less effort than it takes to develop them all from scratch. For more on child themes see http://codex.wordpress.org/Child_Themes.

Pop Quiz – understanding advanced theme features

Q1. What are pretty permalinks?

  1. Images in a theme's design which make the site look pretty.
  2. User-friendly URLs which use post titles instead of numerical IDs.
  3. URLs which are better for SEO.

Q2. Where might you add a widget area to your themes?

  1. In the sidebar.php file.
  2. In the footer.php file.
  3. In the header.php file.
  4. All of the above.

Q3. What steps do you need to take to add a menu to a theme?

  1. Register the menu in the functions.php file, add it to the correct place in the theme (normally in header.php) and set it up in the Menus admin screen.
  2. Register a widget area and then add that to the header.php file.
  3. Add the wp_nav_menu() code to header.php then set up the menu using the Menus admin screen.
  4. Code the links directly into the header.php file.

Q4. I'm building a child theme. My parent theme and child theme both have an index.php and a style.css file. My parent theme has an archive.php file and my child theme has a page.php file. When I visit a category listing on my site, which template file will be used to display it?

  1. index.php from the parent theme.
  2. index.php from the child theme.
  3. archive.php from the parent theme.
  4. page.php from the child theme.
..................Content has been hidden....................

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