{"id":694,"date":"2011-10-18T14:13:17","date_gmt":"2011-10-18T12:13:17","guid":{"rendered":"http:\/\/www.kavdesign.net\/blog\/?p=694"},"modified":"2015-11-05T21:05:38","modified_gmt":"2015-11-05T19:05:38","slug":"how-to-add-extra-sidebar-to-a-wordpress-theme","status":"publish","type":"post","link":"https:\/\/www.kavdesign.net\/blog\/coding\/how-to-add-extra-sidebar-to-a-wordpress-theme\/","title":{"rendered":"How to add extra sidebar to a WordPress theme?"},"content":{"rendered":"<p>To begin the tutorial, extract your theme into a folder and open it to see all the files. If your theme has only one sidebar, then most probably you will NOT find a file called <em>functions.php<\/em> in your theme folder. In that case you will have to create this file yourself. Just open notepad or any other code editor to start a new file. Put this code into that file:<\/p>\n<p><!--more--><code>&lt;?php<br \/>\nif ( function_exists('register_sidebars') )<br \/>\nregister_sidebars(2);<br \/>\n?&gt;<\/code><\/p>\n<p>Or add this code instead:<br \/>\n<code><br \/>\nif (function_exists('register_sidebar')) {<br \/>\nregister_sidebar(array(<br \/>\n'name'=&gt;'Put the name of the sidebar HERE',<br \/>\n'before_widget' =&gt; '',<br \/>\n'after_widget' =&gt; '',<br \/>\n'before_title' =&gt; '',<br \/>\n'after_title' =&gt; '',<br \/>\n));<br \/>\n}<\/code><\/p>\n<p>Save the file as <em>functions.php<\/em> and put it in your theme folder. This piece of code actually tells WordPress to register two sidebars for you (See <em>register_sidebars(2)<\/em> in the code). If your theme has more than one sidebar, you will find the <em>functions.php<\/em>file already present in your theme folder. You just have to edit the number to your requirement and save the file. You can increase this number if you want more sidebars and if your theme\u2019s layout can accommodate it. Now, when you go to your WordPress admin section and browse to the widgets under the menu item called presentation, you will see two sidebars listed there. You can drag your widget items into any of the sidebars.<\/p>\n<p>Now comes the part where we actually build the sidebars. If your theme has only one sidebar, try to locate a file called <em>sidebar.php<\/em> in your theme folder<em>. <\/em>In this example, where we are trying to modify the theme for two sidebars, let\u2019s rename <em>sidebar.php<\/em> to <em>sidebar1.php <\/em>and make a new blank file called <em>sidebar2.php<\/em>. Put this code into <em>sidebar2.php<\/em> and save the file<em>:<\/em><\/p>\n<p><code>&lt;div&gt;<br \/>\n&lt;?php if ( function_exists('dynamic_sidebar') &amp;&amp; dynamic_sidebar(2) ) : else : ?&gt;<br \/>\n&lt;?php endif; ?&gt;<br \/>\n&lt;\/div&gt;<\/code><\/p>\n<p>Or alternatively put this code:<\/p>\n<p><code>&lt;?php if (!function_exists('dynamic_sidebar') ||<br \/>\n!dynamic_sidebar('<code>Put the name of the sidebar HERE<\/code>')) : ?&gt;<br \/>\n&lt;?php endif; ?&gt;<\/code><\/p>\n<p align=\"left\">So, we have the two sidebars ready but they have not been placed in the <em>index.php<\/em> file yet. Both these sidebars need to be called from the <em>index.php<\/em> file in order to include them in your theme. Just open the <em>index.php<\/em> (it could also be in header.php, footer.php, bottom.php or whereever you want to position the sidebar) file from your theme folder and locate the code that calls your sidebar file (<em>sidebar.php <\/em>earlier). It should look something like :-<\/p>\n<p><code>&lt;?php include (TEMPLATEPATH . '\/sidebar.php'); ?&gt;<\/code><\/p>\n<p>Edit this code and change the words <em>sidebar.php<\/em> to <em>sidebar1.php.<\/em><\/p>\n<p>This takes care of the first sidebar. Now take a look at the <em>index.php<\/em> file carefully and find a suitable place to insert the second sidebar. This might involve modifying your layout or adding new divs. Once you find a suitable place, place the following code there :-<\/p>\n<p><code>&lt;?php include (TEMPLATEPATH . '\/sidebar2.php'); ?&gt;<\/code><\/p>\n<p>Save the <em>index.php<\/em> file and now preview your theme. You will see all the widgets that you placed in both your sidebars appearing on your website. If you have not placed any widgets yet, you will not see any change. There might be alignment errors but you will have to fix them yourself. You can add more sidebars in a similar way to your WordPress theme. I hope this tutorial helps some of you.<\/p>\n<hr \/>\n<p><strong>So, now especially for Twenty Fifteen Theme:<\/strong><\/p>\n<p>Add the following to functions.php<\/p>\n<pre class=\"default prettyprint prettyprinted\"><code><span class=\"kwd\">function<\/span><span class=\"pln\"> twentyfifteen_widgets_init2<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\r\nregister_sidebar<\/span><span class=\"pun\">(<\/span><span class=\"pln\"> array<\/span><span class=\"pun\">(<\/span>\r\n<span class=\"str\">'name'<\/span>          <span class=\"pun\">=&gt;<\/span><span class=\"pln\"> __<\/span><span class=\"pun\">(<\/span> <span class=\"str\">'Widget Area Right'<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'twentyfifteen'<\/span> <span class=\"pun\">),<\/span>\r\n<span class=\"str\">'id'<\/span>            <span class=\"pun\">=&gt;<\/span> <span class=\"str\">'sidebar-2'<\/span><span class=\"pun\">,<\/span>\r\n<span class=\"str\">'description'<\/span>   <span class=\"pun\">=&gt;<\/span><span class=\"pln\"> __<\/span><span class=\"pun\">(<\/span> <span class=\"str\">'Add widgets here to appear in your sidebar.'<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'twentyfifteen'<\/span>     <span class=\"pun\">),<\/span>\r\n<span class=\"str\">'before_widget'<\/span> <span class=\"pun\">=&gt;<\/span> <span class=\"str\">'&lt;aside id=\"%1$s\" class=\"widget %2$s\"&gt;'<\/span><span class=\"pun\">,<\/span>\r\n<span class=\"str\">'after_widget'<\/span>  <span class=\"pun\">=&gt;<\/span> <span class=\"str\">'&lt;\/aside&gt;'<\/span><span class=\"pun\">,<\/span>\r\n<span class=\"str\">'before_title'<\/span>  <span class=\"pun\">=&gt;<\/span> <span class=\"str\">'&lt;h2 class=\"widget-title\"&gt;'<\/span><span class=\"pun\">,<\/span>\r\n<span class=\"str\">'after_title'<\/span>   <span class=\"pun\">=&gt;<\/span> <span class=\"str\">'&lt;\/h2&gt;'<\/span><span class=\"pun\">,<\/span>\r\n<span class=\"pun\">)<\/span> <span class=\"pun\">);<\/span>\r\n<span class=\"pun\">}<\/span><span class=\"pln\">\r\nadd_action<\/span><span class=\"pun\">(<\/span> <span class=\"str\">'widgets_init'<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'twentyfifteen_widgets_init2'<\/span> <span class=\"pun\">);<\/span><span class=\"str\">'<\/span><\/code><\/pre>\n<p>After that copy sidebar.php from the theme, and add the following to it:<\/p>\n<pre class=\"default prettyprint prettyprinted\"><code><span class=\"tag\">&lt;div<\/span> <span class=\"atn\">id<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"anothersidebar\"<\/span> <span class=\"atn\">class<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"sidebar\"<\/span><span class=\"tag\">&gt;<\/span>\r\n    <span class=\"pun\">&lt;?<\/span><span class=\"pln\">php <\/span><span class=\"kwd\">if<\/span> <span class=\"pun\">(<\/span><span class=\"pln\"> is_active_sidebar<\/span><span class=\"pun\">(<\/span> <span class=\"str\">'sidebar-2'<\/span> <span class=\"pun\">)<\/span> <span class=\"pun\">)<\/span> <span class=\"pun\">:<\/span> <span class=\"pun\">?&gt;<\/span>\r\n    <span class=\"tag\">&lt;div<\/span> <span class=\"atn\">id<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"widget-area\"<\/span> <span class=\"atn\">class<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"widget-area\"<\/span> <span class=\"atn\">role<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"complementary\"<\/span><span class=\"tag\">&gt;<\/span>\r\n        <span class=\"pun\">&lt;?<\/span><span class=\"pln\">php dynamic_sidebar<\/span><span class=\"pun\">(<\/span> <span class=\"str\">'sidebar-2'<\/span> <span class=\"pun\">);<\/span> <span class=\"pun\">?&gt;<\/span>\r\n    <span class=\"tag\">&lt;\/div&gt;<\/span><span class=\"com\">&lt;!-- .widget-area --&gt;<\/span>\r\n<span class=\"pun\">&lt;?<\/span><span class=\"pln\">php endif<\/span><span class=\"pun\">;<\/span> <span class=\"pun\">?&gt;<\/span><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To begin the tutorial, extract your theme into a folder and open it to see all the files. If your theme has only one sidebar,<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.kavdesign.net\/blog\/coding\/how-to-add-extra-sidebar-to-a-wordpress-theme\/\">Continue reading<span class=\"screen-reader-text\">How to add extra sidebar to a WordPress theme?<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[9,82],"tags":[211,210,212,209,150,418],"class_list":["post-694","post","type-post","status-publish","format-standard","hentry","category-coding","category-wordpress","tag-additional","tag-extra","tag-position","tag-sidebar","tag-widget","tag-wordpress","entry"],"_links":{"self":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts\/694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/comments?post=694"}],"version-history":[{"count":15,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions"}],"predecessor-version":[{"id":1495,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions\/1495"}],"wp:attachment":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/media?parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/categories?post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/tags?post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}