For a yoga school with different centers in various cities I needed a variable sidebar, so each city has its own sidebar displayed on all their specific pages and posts. There are good plugins to achieve this, but I keep the number of plugins as little as possible. So whenever I can solve something in the child theme files, I prefer that version, and since DIVI offers the creation of sidebar areas, the automatic assignment to different users – one for each city – was a question of just a few lines of code. If you use the created sidebars together with the sidebar module of the DIVI Builder, you don’t need any code, you just select them there. But I am using sidebars also on regular pages and posts and I like them to go all the height, read this article to see what I mean and how I achieve this in my child theme.
To create additional sidebars in DIVI, go to Appearance / Widgets and find the box on the right side. Take the note of “Naming your widget area “sidebar 1”, “sidebar 2”, “sidebar 3”, “sidebar 4” or “sidebar 5” will cause conflicts with this theme” serious, choose an appropriate name and click the Create button. Now your sidebar is ready to be equipped with the widgets of your choice. Now, to display the sidebar according to the author of the page or post, we just need to add a few lines to the sidebar.php (of course in best case into its copy in our child theme to not loose the changes during next update). Replace the line
1 |
<?php dynamic_sidebar( 'sidebar-1' ); ?> |
with
1 2 3 4 5 6 7 8 9 10 11 |
<?php $author = get_post_field('post_author', get_the_ID()); if ($author == '2') { dynamic_sidebar( 'berlin' ); } elseif ($author == '5') { dynamic_sidebar( 'freiburg' ); } else { dynamic_sidebar( 'sidebar-1' ); } ?> |
Leave the else condition untouched, but replace “berlin” and “freiburg” and the author ID’s with your own. If you need more, copy another elseif section. If you need less, delete the elseif section. That’s it, no need to bloat up your website with another plugin.
Hi Sundari,
Thank you for this article. I noticed this was written 4-years ago, so I’m wondering if this will still work; I’m afraid to freeze my site. What I want to do is have a different sidebar for each of my authors.
I hope you are well,
Lyn
Hi Lyn, good question, I haven’t used it myself for many years. The code will definitely cover your need that you described, and since you anyhow will need to do it in a child-theme, I assume you have FTP access to your website and can simply try it out. If it doesn’t work, you simply remove the code again from your file. But yes I suggest to only play with this if you can restore your php file through FTP and do not depend on a plugin that does it from within WP, because then if your site should freeze you can only fix it by accessing the file directly. Wish you success :)