Time for action – adding widget areas to sidebar.php

To make our widgets work in the sidebar, we'll need to replace some of the existing code with a WordPress tag to display the correct widget area.

  1. Open your theme's sidebar.php file.
  2. Delete all of the code in the file and replace it with the following:
    <aside class="sidebar right third">
    <?php if ( is_active_sidebar( 'sidebar-widget-area' ) ) : ?>
      <div class="bdr grd-vt-main rnd shdw-centered">	
        <?php dynamic_sidebar( 'sidebar-widget-area' ); ?>
      </div>
    <?php endif; ?>
    </aside>
  3. Save the sidebar.php file.

What just happened?

We added a widget area to our sidebar. Let's have a look at the code in detail:

  • Firstly, the <aside> element to contain our sidebar—which is the same as in our static mockup
  • The next line checks if there are any widgets in the widget area with the ID of sidebar-widget-area
  • If that's the case, it displays the contents of the widget area, using the dynamic_sidebar tag with the ID of our sidebar
  • It then closes all the relevant tags

Now let's see what the site looks like in the browser:

What just happened?

At the moment the sidebar is empty because we haven't added any widgets. We'll do that in a moment but first let's add widget areas to our footer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.67.251