In this tutorial you will learn how to create a WordPress theme almost from scratch via the theme skeleton Underscores.
Download Underscores
- Go to http://underscores.me/ and enter the name of your theme in the input box.
- Then click enter, and you’ll get a zip with all the theme files in a folder.
- Install the theme via the Dashboard in WordPress.
Now you have a very basic theme. There is a menu, but it’s horrible, or so it seems. In fact you’ve got a dropdown menu – so that’s kind of a head start. And that’s the idea of Underscores. From here it’s a matter of styling, but where are the files we need?
Style.css
Have a look in the style.css file. Here your theme name and author is defined. Add your name and the URI to the website, or even better to your Github repository. Since you’re working on a home hacked theme, you may do stupid things. So enter your credentials e.g. (see my sample style.css here):
Theme Name: Abracadabra Theme URI: https://github.com/asathoor/underscores-and-sass Author: Per Thykjaer Jensen and Underscores.me Author URI: https://multimusen.dk Description: The 2017/18 theme for WordPress based on Underscores. Features: SASS, Gulp, Bower, Font Awesome, jQuery. Version: 0.5 License: GNU General Public License v2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html Text Domain: petj-17 Tags: custom-background, custom-logo, custom-menu, featured-images, threaded-comments, translation-ready, responsive, two-columns, custom-header-image
Sidebar – Content or Content – Sidebar
Since Michael Heileman designed the Kubrick theme in 2004 the standard blog design became something like a sandwhich with a header and footer as the buns. Between the header and footer you’d see some content and a sidebar. So the blog designer could choose between content-sidebar or sidebar-content. Of course you don’t have to do this, but the standard WordPress design still use the magick formula: header.php, footer.php, sidebar.php – and a filename. If there are no other files, WordPress will use index.php as a fallback. All of these files are found among the Underscores files.
Underscores made it easy to create a basic design, with content and a sidebar to the right or left of the content.
Layouts
In the folder layouts you’ll find two files with the CSS for the basic layout:
- content-sidebar.css
- sidebar-content.css
So if you want a classic blog layout, you can copy-paste the content of the one you fancy to style.css. Here is the CSS from content-sidebar.css:
.content-area { float: left; margin: 0 -25% 0 0; width: 100%; } .site-main { margin: 0 25% 0 0; } .site-content .widget-area { float: right; overflow: hidden; width: 25%; } .site-footer { clear: both; width: 100%; }
Now you have a traditional blog design.
Responsive Styles
Of course these styles are not responsive at all. You still have to refine your design with media queries and so on. Since the CSS in media queries are quite redundant a SASS mixin may come in handy (see this sample).
@media screen and (min-width: 100px) and (max-width: 760px) { @include responsive(); // sidebar to bottom body { font-size: 123%; } }
Custom Header Image
The Underscores theme does not support a custom header image. Many themes give you this option, so how do you implement the feature? First you have to add this to the functions.php file:
$args = array( 'flex-width' => true, 'flex-height' => true, 'default-image' => get_template_directory_uri() . '/images/clouds.jpg', ); add_theme_support( 'custom-header', $args );
Then open the header.php file, and add the following where you want the image to appear:
<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="">
From now on the user is able to choose his own header images from the media. This short sample also demonstrates, that you can edit any file in the theme.
Custom Pages
In WordPress custom pages have to follow a certain naming convention. You can see the system in the template hierarchy.
Let’s say that you want a custom design on the front page. In the template hierarchy you can see that the WordPress will look for the frontpage code in the file front-page.php.
I’d like to design my frontpage in Siteorigin’s PageBuilder. So I need a frontpage that does not interfer with Pagebuilder. And I don’t want a headline. A fast head start is to copy a file and use it as a model. Remove what you don’t want. Add what you need. In this case the result was extremely simple:
get_header(); ?> <div id="primary" class="content-area"> <main id="main" class="site-main"> <?php // the loop while ( have_posts() ) : the_post(); get_template_part( 'template-parts/content', 'frontpage' ); endwhile; // End of the loop. ?> </main><!-- #main --> </div><!-- #primary --> <?php //get_sidebar(); get_footer();
The loop will get template parts from the file ../template-parts/content-frontpage.php. Here is the loop part for the full width PageBuilder content:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php the_content(); ?> </article><!-- #post-<?php the_ID(); ?> -->
This part of the loop will print the content, but not the headline. If you want the headline feel free to add the_header() and similar loop tags (see this article).
It’s a Theme
With Underscores you’ll learn what it takes to create a WordPress theme almost from scratch. Underscores is the basis of many themes – among them the core WordPress theme Twenty Eleven.
Leave a Reply