Home » Tutorials » Wordpress » How to create a Sidebar for your WordPress Theme

How to create a Sidebar for your WordPress Theme

creting wordpress sidebar

Most of the wordpress themes use only one dynamic sidebar. So if you want to create a professional wordpress template, which provides users with higher amount of customizing options, you need to add a variety of sidebars for different pages. You can also put a new sidebar in the header, footer or in any other area of your template.

Let’s first describe what is a dynamic sidebar. This is a container of widgets, where you can drag and drop the wordpress widgets you want to use.

creating wordpress sidebar
WordPress widgets menu

Adding a Sidebar to our WordPress Theme

First we need to edit our functions.php file. It must be in the main directory of your wordpress theme. If in some way your template doesn’t have a functions.php file, you should create one.

After you open it, make a search for register_sidebar functions. It should look like this:

if ( function_exists('register_sidebar') ) {
register_sidebar(array(
 'name' => 'Main Sidebar',
 'before_widget' => '<li id="%1$s">',
 'after_widget' => '</li>',
 'before_title' => '<h2>',
 'after_title' => '</h2>',
));
}

In case you don’t have sidebars in your tempate, add the above code in functions.php.

To create a second sidebar for your wordpress theme (for example footer sidebar), paste the following code:

if ( function_exists('register_sidebar') ) {
register_sidebar(array(
 'name' => 'Footer Sidebar Area',
 'id' => 'footer-sidebar',
 'description' => 'Sidebar for the footer of your theme',
 'before_widget' => '<div style="height: 250px"></div><li id="%1$s">',
 'after_widget' => '</li>',
 'before_title' => '<h2>',
 'after_title' => '</h2>',
));
}

With this piece of code we told our theme that we are adding a second sidebar area.

Creating a separate sidebar file

Once you’ve registered the new sidebar, you’ll want to display it within your theme. The WordPress function for this purpose is called dynamic_sidebar().

WordPress has a standard file for the main Sidebar called sidebar.php. But if we want to create a second one, we need to create a separate file sidebar-footer.php.

Tip: It is important to use the same order: sidebar-something.php

This is a basic code to place in your new sidebar-footer.php file:

<aside id="sidebar">
<ul>
<?php dynamic_sidebar( 'footer-sidebar' ); ?>
</ul>
</aside>

If you want to use the full version of the sidebar, with all the extras one professional sidebar should have simply copy the content of your theme’s sidebar.php file, or use one from another theme as an example.

Calling the Additional Sidebar

To call the main sidebar in your index.php file for example, you can simply use the

<?php get_sidebar(); ?>

But when your trying to call some of the additional sidebars, you must use the following code:

<?php get_sidebar('footer'); ?>

It is important to note that when you call a sidebar from sidebar-footer.php, you use get_sidebar('footer');

If you did everything correctly you can drag some text widget to your new sidebar position and test it.

Wordpress Sahifa Sidebar
Sidebar example
Most of the wordpress themes use only one dynamic sidebar. So if you want to create a professional wordpress template, which provides users with higher amount of customizing options, you need to add a variety of sidebars for different pages. You can also put a new sidebar in the header,&hellip;

Review Overview

0%

User Rating: 4.01 ( 7 votes)
0

About Georgi Dyulgerov

Georgi Dyulgerov

Founder of fTutorials.com - the website aiming to keep you in touch with the latest technologies in the brand. Also a Freelance web developer.

Check Also

lost wordpress password

How to reset WordPress password from PhpMyAdmin

Nowadays, it might be very common to forget the password for your own blog. When ...

4 comments

  1. Здравейте :) В sidebar.php нужно ли е да се пише някакъв код,защото аз регистрирах 3 sidebar-а functions.php ,но поставих кода им там където исках 2 във footer.php и един в page.php ,показва се календар,когато се отвори страница.sidebar.php ми е празен. Отскоро правя wordpress теми и имам някои въпроси.

    • Здравейте, да например < ?php get_sidebar('footer'); ?>, както съм го описал. След това от Widgets трябва да си поставите джаджите, които искате да използвате. Успех :))

  2. Oh my goodness! Impressive article dude! Thank you so much, However I am going through problems with your
    RSS. I don’t understand why I am unable to join it.
    Is there anybody getting identical RSS problems? Anyone who knows the answer will you kindly respond?
    Thanks!!

Leave a Reply

Your email address will not be published. Required fields are marked *


Loading Facebook Comments ...