Post Custom Sidebars: How To Use a Different One for Each Post And Page
You probably didn’t know it, but you are not forced to always display the same sidebar on all your WordPress posts and pages. There is an extremely simple way to display custom sidebars on certain posts by implementing the following code.
First of all, you need to create your custom sidebar files and upload them in your own theme folder.
Then, you need to open your
page.php files in your WordPress theme folder and search for the code:
<?php get_sidebar(); ?>
Simply replace this line by the following code:
<?php $sidebar = get_post_meta($post->ID, "sidebar", true); get_sidebar($sidebar); ?>
From this point, when you’ll publish a new post or a new page, create a
custom field named
sidebar in the post.
As an example, if you give it the value
sidebar-3213, WordPress will automatically use the file named
sidebar-3213.php as a sidebar.