Adding a Sidebar to WordPress - Tutorial 18

To start, I'll say, that the sidebar determines where widgets will be displayed and in order for you to have the ability to display widgets in your template, you will need to enable sidebar support.

To enable, go to the functions.php file and add the code there.

Let's take a look at all the attributes, entered into the $args variable:

  • id – unique sidebar id, when creating other sidebars this id must not be repeated;
  • name – sidebar title;
  • description – sidebar purpose description;
  • class – a class that can be added to style the sidebar;
  • before_title – text or tags that should be inserted before the title;
  • after_widget – text or tags to be inserted after the title;
  • before_widget – text or tags before the widget;
  • after_widget – text or tags after the widget.

before_widget and after_widget – used to create a widget wrapper.

After adding the code, the Widgets item will appear in the admin panel (Widgets).

wordpress create sidebar widgets курсы по wordpress

Let's look at the sidebar itself and analyze it point by point, what is on it:

Creating a new sidebar for the left column

  1. name‘ => __( 'Sidebar', ‘striped’ ) – sidebar title;
  2. description‘ => __( ‘Left column’, ‘striped’ ) – sidebar description;

Drag to our sidebar widget:

wordpress insert category widget

Until our sidebar displays an active widget, you can only work with it in the admin panel. For the widget to be displayed, you need to add a special code in the template sidebar-left.php.

Add code at the top of the sidebar-left.php template.

After adding the code, a widget should appear on your site.

display widget wordpress in themplate

If we now remove the categories widget and leave the sidebar empty, then in place of the previously displayed widget, nothing will be displayed except for the sidebar wrapper.

empty widget wp wordpress courses

This point can be fixed., slightly modifying the widget output code.

In the functions.php file, change the code to this one:

In the sidebar-left.php file, change the code to this:

Now consider, how to completely hide the sidebar and display only if it has widgets.

In the functions.php file

In the sidebar-left.php file

All for today, further you independent work. You need to display the sidebar on the right, if it doesn't work, you can download my source files and peek, how did i do it.

zip wordpress theme archive


Buy WordPress hosting
WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

2 Responses

  1. John Deere says:

    Very nice post, I surely love this site, keep it up.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal