Move WordPress Sidebar Widgets Above Content in Responsive Mode

By | October 12, 2016

Do you have a sidebar that contains essential widgets that your visitors must see when they visit your site from a mobile device? Perhaps a search box, product categories, an advertisement, or something else?

A lot of WordPress themes display the sidebar below the content on mobile devices, so visitors have to scroll all the way to the bottom to see them. There are many tutorials that explain how to move the sidebar above the content, but maybe you only want to move one or two of the most critical widgets to the top. This post will explain how to do that!

Create a Child Theme

We are going to make changes to the theme’s PHP scripts and CSS stylesheets. You don’t want the changes to be wiped out next time you install a theme update, so create a child theme if you haven’t done so already. The child theme’s directory will contain a copy of the PHP files we change, and those files will contain the customizations we make.

Register a New Sidebar

The next step is to register a new sidebar with WordPress. This sidebar will be displayed above the content on mobile devices, and you will be able to insert just the widgets you want your visitors to see.

Below is an example of what the code to register a sidebar looks like. If you want the sidebar to look the same as your current one, then find this code in the parent theme’s functions.php and copy and paste it into the functions.php file of your child theme. Change the name, id, and description.

In this code we declare a new function called above_content_sidebar() in which we pass an array to the register_sidebar() function. In the last line, we call the add_action() function to include the sidebar in widgets_init.

Note the id of the sidebar (in this case ‘top-sidebar’) because you will use it in the following steps.

Create a New Sidebar File

In this step, we will create a new sidebar PHP file in the child theme’s directory. We will make calls to it later when we modify more of the theme’s files.

Copy and paste the sidebar.php file from the theme’s parent directory into the child directory. Rename it so that it begins with “sidebar-” and then give it a unique name. “sidebar-top.php”, for example. The file must begin with “sidebar-” due to the way it is called with the get_sidebar() function. This will be demonstrated later.

In the code itself, replace the name of the sidebar with the id you created in the previous step. For example, the new name in the code below is ‘top-sidebar’. Also, consider adding a new CSS class to the DIV tag containing the sidebar, so that you can assign it unique display rules (ie: to remain visible in mobile mode). In this example I added the class ‘widget-area-top’ to the ‘topsidebar’ DIV. We will write CSS for this class in the last step.

Add Widgets to the Sidebar

Now the new sidebar should be visible from the WordPress Administration panel. Navigate to “Widgets” under “Appearance” and you should see the new sidebar there. Add the widgets that you want your visitors to see.

wordpress_admin_sidebars

Display the New Sidebar

To actually display the new sidebar on your website, you need to call the get_sidebar() function from within your theme files. Without any arguments, the get_sidebar() function loads the default sidebar. If you want to display a specific sidebar, then you must pass its name to the function. For example, get_sidebar(‘top’).

The sidebar’s name is the name of the file we created earlier, minus the “sidebar-” portion. This is the naming convention WordPress uses to identify sidebar files.

Remember to copy any PHP scripts you will change to the child theme’s directory and modify them there.

Here’s an example of calling the new sidebar from page.php. We want this sidebar to appear above the content, so the get_sidebar() function is called before the posts’ while loop. The original sidebar remains visible at the bottom.

Add get_sidebar(‘sidebarname’) to any other theme files you want it to be visible in (For example: index.php, page.php, single.php, search.php, category.php, etc.)

You should now be able to see the new sidebar as you navigate through your WordPress site. The last step is to make the sidebar responsive so that it is hidden unless it is being viewed from a mobile device.

Make it Responsive

We will now edit the CSS stylesheet in your child theme’s directory to make the new sidebar responsive to mobile devices.

The changes below keep the sidebar hidden until resolution passes 768 pixels. Make sure that the CSS class (in this case, ‘widget-area-top’) matches the class name you assigned to the sidebar’s DIV.

Refresh the page and the sidebar should disappear. However, the sidebar should be visible from a mobile device.

If you are having any difficulties, feel free to leave a comment below or contact me for assistance.

References

http://themegrill.com/support-forum/topic/widget-above-content/

https://www.tastyplacement.com/add-sidebar-wordpress

https://themify.me/docs/second-sidebar

https://www.ostraining.com/blog/wordpress/custom-sidebar/

 

One thought on “Move WordPress Sidebar Widgets Above Content in Responsive Mode

Leave a Reply

Your email address will not be published.

*