WordPress Theme

Writing our WordPress Theme is relatively simple.

Requirements:

Before we begin, we need to know about the following set of requirements.

  • We need to have full knowledge of WordPress setup, which is either on the local host or live hosting. Click here to know more about WordPress.
  • A conceptual design, either HTML CSS or PSD that follow throughout the development process.
  • Basic knowledge of PHP Programming.

 Let`s Start:

The first thing we need to know that everything we do in WordPress is inside the wp-content directory. When we open the wp,-content->theme file, we will find some default WordPress themes like twentyfifteen, twentythirteen, etc.

wordpress theme

We need to create a new file with any name we prefer.

We will call it a “newtheme.”

A WordPress theme contains at least two files, which are index.php and style.css.

Now go into the newtheme folder and create these two files.

In the style.css, we will insert the following comment, which is used to tell the user about the theme details and Meta information.

/* 
 Theme Name: NewTheme  
 Description: WordPress First Theme
 Version: 0.8  
 */ 

Go to the WordPress Dashboard, click on the Appearance-> Themes.  We will find NewTheme in the themes collection.

wordpress theme 1

Now, open index.php in the text editor and write the following code.:

 
 
 

Hello WordPress Theme.

Sections:

To develop a standard WordPress Theme, we need to divide our work into sections. It is not necessary to do everything in index.php, but a good programming practice involves standardized. We will divide our Theme into four sections, i.e., header, footer, sidebar, and body. These sections are created into four different files, namely, header.php, footer.php, sidebar.php, and content.php.

 Let`s do our Theme Header (Header.php) :


 
 
 
 
  
  <?php echo get_bloginfo( "name" ); ?>  
  
  
  
  
  
  
 <?php echo get_bloginfo( "name" ); ?>  

This is embedding HTML code into PHP code.

With some additional changes, it becomes:


  
  
 
  
  <?php echo get_bloginfo( "name" ); ?>  
 
  
  
  
  
  

The above code is used to get the template directory, CSS,JS etc additional resources can be located.

The above code is used to echo the homepage url.

wordpress theme 2

Footer.php:

 
  
  
  
  

The above code is used to place the site description.

sidebar.php: Often sidebars display archive links, recent posts, social media accounts, etc. In our Theme, we’ll create archive links and social media links.

 

Integration:

In index.php file, we will integrate all the sections. We have to clearly put these sections. Let`s see how I have done it.

 
 
  • In the above code, we have get_header(), get_sidebar() and get_footer() that all are predefined functions used for embedding sections.

And for the custom section like content.php, we have embedded with the following code:

Style.css: Let`s add some charm with CSS.

/* 
  Theme Name: NewTheme 
 Description: WordPress First Theme 
 Version: 0.8
 */
 body{
 background-image: linear-gradient
(to right, #eea2a2 0%, #bbc1bf 19%, #57c6e1 42%, #b49fda 79%, #7ac5d8 100%);
 }
 nav.navbar .navbar-brand .site-branding { 
 margin: 0; 
 } 
 footer.site-footer { 
 background-color: black; 
 color: #fff; 
 padding: 40px 0 20px 0; 
 } 
 footer.site-footer a { 
 color: #fff; 
 } 

The Loop:

The Loop is a functionality through which we can insert content into our theme.


 
 
 

content.php:

 
  

  • In the above code, anything which is inside the Loop will be repeated, till the page done with all of its posts.

Changed the (index.php):

 
 

The Loop inside the index.php file is calling the content.php file every time the page has a new updated post. Inside the content.php file, we have checked if the current post is_single(). This condition is used to hold true if the current page contains only a single post to Loop through. If it is not single, we wanted to link to the post via its title. So we used get_permalink() to get the URL for the particular post. If the page is single, there is no need to link it, and therefore, we have used only the_title() function.

We have displayed the_date() which is used to tell the user when the article/post is published and it’s the_author().  At last, we have used the same concept of is_single() which is used to display the_excerpt() or the_content() of the post.

So, it was easy, with the little charm of CSS, we have got the following result:

wordpress theme 4

  • We are ending this post, but we need to know there is still a lot to learn about WordPress.
  • It was a simple example, but a standard theme would be much complex.

Related Topics

PHP strcoll() Function

PHP strcoll() Function The strcoll() function in PHP compares two strings. This function is case-sensitive but not binary-safe. It uses the current locale for doing the comparisons. Syntax strcoll ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter...

1 minute read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

PHP $ and $$ Variables

PHP $var variable is a normal variable that stores any value. PHP $$var variable is a reference variable that stores the value of$var inside it. Let us take an example. Example 1 <?php $x =...

1 minute read.

PHP Environment Setup

To install PHP, We have to installed AMP(Apache, MySQL and PHP) software. There are various AMP software available in the market. XAMPP (Cross-Platform, Apache, MySQL, PHP and Perl) for windows. ...

2 minutes read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.

PHP array_unshift() Function

PHP array_unshift() Function The array_unshift () function in PHP prepends one or more elements to the beginning or front of an array. It returns the new number of elements in the array. Syntax array_unshift ( array &$array [, mixed $... ] ) Parameter array(required)- This array represents...

1 minute read.

PHP join() Function

PHP join() Function The join() function of PHP returns a string from the elements of an array. This function is an alias of the implode() function. Syntax join(separator,array) Parameter separator(Optional)- Specifies what to put between the array elements. The default...

1 minute read.

PHP array_intersect_ukey() function

The array_intersect_ukey () function in PHP is used to compute the intersection of arrays with the help of a callable function on the keys for comparison. It returns an array containing all the...

1 minute read.

PHP asort() function

PHP asort() function The asort() function in PHP is used to sort an array while maintaining the index association. Syntax asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  array(required)- This parameter represents the input array to sort. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP fprintf() Function

PHP fprintf() Function The fprintf() function in PHP outputs a formatted string. This function operates as printf() but accepts an array of arguments, rather than a variable number of arguments. Syntax vprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to...

4 minutes read.

PHP strcmp() Function

PHP strcmp() Function The strcmp() function in PHP compares two strings. It is a binary-safe case-sensitive string function. This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each...

1 minute read.

PHP str_split() Function

PHP str_split() Function The str_split() function in PHP splits a string into an array. String str_split ( string $string [, int $split_length = 1 ] ) Parameter string(required)- This parameter represents the input string. split_length(optional)- This parameter signifies the maximum length of the chunk. The default value is 1. Return This function...

2 minutes read.

PHP sizeof() Function

PHP sizeof() Function The sizeof() function in counts all elements and return the size of the array. This function is an alias of count(). Syntax sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)-...

1 minute read.

PHP strval() function

PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...

1 minute read.

PHP stripos() Function

PHP stripos() Function The stripos() function in PHP finds the position of the first occurrence of a substring in the specified string. It is a case-insensitive a binary-safe function. The related functions are as follows: strripos() Functionstrpos() Functionstrrpos() Function Syntax stripos ( string $haystack , mixed $needle [, int $offset...

1 minute read.

PHP array_udiff_assoc() Function

PHP array_udiff_assoc() Function The array_udiff_assoc () function in PHP computes the difference between the specified arrays with additional index check and compares the data by a callback function. It returns all the values from array1 that are not...

1 minute read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.

PHP array_shift() Function

PHP array_shift() Function The array_shift() in PHP shifts the first value of the array off. This function returns the same value after shortening the array by one element and moving everything. Syntax array_shift ( array &$array )  Parameter array(required)- This parameter represents the input array Return This function returns...

1 minute read.

PHP ksort() Function

PHP ksort() Function The ksort() function in PHP sorts an array by key while maintaining the keys. Syntax ksort(array & $array[,int $sort_flags = SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This parameter is...

2 minutes read.

How to use Date and Time in PHP

HOW TO USE DATE AND TIME IN PHP: PHP time and date function  informs us about current date and time. it can be manipulated also. How time() function works: The time() function...

2 minutes read.