The smart way to disable your sidebar
If you find yourself making a new WordPress template for pages that are identical to others on your site but just don’t have a sidebar, consider this technique instead which lets you turn the sidebar off selectively on a page by page basis.
Add this to functions.php
1 2 3 4 5 6 |
<?php // look to see if we've disabled sidebar in a custom field, if not show it $disableSidebar = get_post_meta($post->ID, 'disableSidebar', $single = true); if ($disableSidebar !== 'true') { get_sidebar(); } ?> |
index.php, page.php … etc.
Next find the file that has the sidebar in it. If you want this to work on all blog posts, it’s probably single.php, or if you add it to index.php and/or page.php it’ll work on other types of pages. Once you find your sidebar you need to put a bit of code before it and a bit of code after it. In the beginning bit looks to see if the custom field is set on your page, the second bit is just the end of the ‘if’ statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!--BEGIN: sidebar~main--> <?php // to disable this sidebar on a page by page basis just add a custom field to your page or post of disableSidebar = true $disableSidebar = get_post_meta($post->ID, 'disableSidebar', $single = true); if ($disableSidebar !== 'true'): ?> <!--BEGIN: my sidebar, yours may look a little different--> <aside id="sidebar-main"> <h1>Main Sidebar</h1> <?php dynamic_sidebar('sidebar-main'); ?> </aside> <!--END: my sidebar--> <?php endif; ?> <!--END: sidebar~main--> |
what it does
It checks to see if you have a custom field set in your post to disable the sidebar and if you do it turns off the sidebar
how to use it
Just make a new custom field for your page called disableSidebar and set it to true. That’s it.
Topics covered: codecustom fieldsdisable sidebarwordpress