What is a Responsive Theme?
With so many devices with different resolutions, screen sizes, and operating systems available in the market, the need for responsive websites and applications has become essential for anyone building an online presence.
Web developers, marketers, and designers from around the world know that there is a huge potential if your website is responsive and can be viewed, navigated, and browsed through easily on any device, whether mobile or desktop regardless of any resolution and aspect ratio.
Responsive themes are an approach to web designing in which elements of a website self-adjust according to screen sizes and browsers for better readability, user-experience, and load time. Developers and theme designers are cashing in on the concept of responsive websites.
Back in November 2013, Matt Cutts, the head of Google’s webspam team, released a video explaining that responsive design won’t negatively affect you in the SERPs. Further, Google has also published their guideline on mobile-friendly websites that contains details in entirety about how Google treats responsive websites.
As more and more smartphones, tablets, and other mobile devices are made available to the public, the use of desktops and laptops has seen a declining trend. This means people are using their smartphones to search just about anything: from news to local stores, from flight inquiries to latest movies. This rising trend presents a big business opportunity for the online community.
WordPress and Responsive Web
WordPress is the most popular CMS that is used by whopping 27% of websites on the Internet. It enjoys support from a strong community of open-source developers and designers coming from all walks of life. With whopping of the responsive web, most renowned theme developers adapted to responsive designs and introduced easy-to-understand frameworks, like Redux Framework, Carrington Core, Bootstrap etc., that can be used to create a responsive WordPress theme from scratch.
So, what is this Bootstrap?
Bootstrap is a toolkit that helps in the development of complex web applications. It was developed by developers at Twitter and was made available to the open-source community. Bootstrap is lightweight as it is coded in LessCSS.
Now, let’s get to work!
There are only following 8 steps involved to create a WordPress Responsive site based on Bootstrap 3.
- Step 1: Unpacking Bootstrap
- Step 2: Configuring Bootstrap
- Step 3: Copying the Code
- Step 4: Setting up HTML Template
- Step 5: Setting the Header and the Footer
- Step 6: Displaying the Featured Posts
- Step 7: Listing Your Categories
- Step 8: Display the Latest Posts and Authors
To go through this tutorial, you need to have a working knowledge of the following things:
- How FTP and HTML works
- How to edit, create and upload PHP files on your server
- How to navigate around using WordPress admin dashboard
Step 1: Unpacking Bootstrap
- First of all, install WordPress on your domain.
- Then download and unzip Bootstrap from http://getbootstrap.com/
- Once done, connect to your hosting server by using an FTP client like FileZilla.
- Navigate to wp-content > themes.
- Create a new folder in themes directory called BootSTheme and upload the contents of unzipped Bootstrap in this folder.
- Almost all of the WordPress installations contain the following files:
Now, make 4 empty files using the filename given above.
Step 2: Configuring Bootstrap
Open style.css and paste the following code.
/* Theme Name: MyTheme Theme URI: https://cloudways.com Description: Mytheme Built on bootstrap Version:1.1 Author: Ahsan Parwez Author URI: https://cloudways.com */
These are basically comments that provide description and details about the theme. If you want, you can place your own comments too.
Step 3: Copying the Code
For this tutorial, we are not going to use all of the CSS and JS files provided in the bootstrap package, we are going to copy the bootstrap.min.css code into the style.css. Your style.css file should look like this.
Step 4: Setting up HTML Template
We need to have a basic HTML template to work with. You can use this HTML theme for your purpose.
WordPress has built-in functions get_header() and get_footer() that by default call the files header.php and footer.php respectively. What we are going to do is cut the HTML code from top till the first container div and paste it into our header.php file, after which the file should like the image given below.
The footer.php file will contain the rest of the code:
If we activate this theme and load it on our website, we are not going to see anything yet, because index.php does not contain anything. To load header and footer, we are going to use WordPress built-in function to call these elements by pasting the following code in index.php:
<?php get_header(); ?> <?php get_footer(); ?>
Now header and footer elements will load on our website, but we will get a basic page that is without any kind of styling.
Step 5: Setting the Header and the Footer
In the header.php file, we are going to import the Bootstrap stylesheet by using the WordPress function echo get_stylesheet_uri() (read more at WordPress Codex), this is going to import the style.css styling onto our website and we are going to see a top menu bar now.
WordPress is known for its customization and plugins. To tell WordPress where to place the plugins hooks, we are going to paste <?php wp_head(); ?> and <?php wp_footer(); ?> in header.php and footer.php files. Also, to set dynamic titles of the website, we are going to use wp_title(); function in the header.php file between the <title> tags.
<title><?php wp_title(' | ',true,'right'); ></title>
The above code will call the title of the post separated by ‘|’ then the site name. The Boolean true will display the title. If you set it to false, it will just return the value and not display it. ‘right’ defines the location of post title to be right of the separator.
Step 6: Displaying the Featured Posts
Now, we are going to call posts dynamically onto our homepage index.php displayed on top, just like featured posts that we see on many WordPress powered sites.
Write the following code into your index.php:
<?php query_posts('posts_per_page=1'); while(have_posts()) : the_post(); ?> <div> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p><?php the_excerpt(); ?></p> </div> <?php endwhile; wp_reset_query(); ?>
We are going to use the while loop and set the number of posts to one by using post_per_page, this piece of code will only display the latest blog post on top of the page, and after the loop is closed, the query_posts resets.
The class jumbotron is defined in our bootstrap.min.css file. We are going to use it to style our featured posts by using <h2> tags and the_permalink(); function. The hyperlink is created on the post title, and the_permalink(); function links to the URL of the entire post. To show an excerpt of the post, we can use another built-in WordPress function that is the_excerpt();
Step 7: Listing Your Categories
Next, we are going to list our categories on the left of the homepage. We will do this by creating many instances of div styled with the classes of bootstrap and by WordPress function wp_list_categories();.
Paste the following code:
<div class="panel panel-default panel-body"> <div> <div> <ul> <?php wp_list_categories('orderby=name&title_li='); ?> </ul> </div> </div> </div>
This will list the categories by name with a nice hover effect.
Step 8: Display the Latest Posts and Authors
Finally, we are going to show latest blog posts on the homepage. We are going to start another div tag and under this div, we will use similar while loop technique that we used in the featured post, but we are not going to limit it with query_posts();.
<div> <?php while(have_posts()) : the_post(); ?> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <p><?php the_excerpt(); ?></p> <p> posted by <?php the_author(); ?> <?php endwhile; wp_reset_query(); ?> </div>
WordPress functions the_author(); and fetch the username of the author of the post. We can use it to dynamically display the name of author with every post.
Once you have complete everything successfully, you will have a page like the one you see in the image shown above.
[You may want to read: 5 Spectacular Plug-Ins For Making WordPress Websites Mobile Responsive]
Quicker WordPress With Cloudways
WordPress, itself being search engine friendly, can really benefit with responsive design. At Cloudways, we believe in delivering ultimate performance. Our Cloud Platform allows you to launch secure WordPress servers within minutes. Your website will be hosted on a highly optimized stack of Apache+Nginx+Memcached+Varnish that cuts load times substantially.