Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Different Sidebars for each page
#1
I am a newbie and whilst I've managed to set up 3 pages etc I do not understand how to have separate sidebars for each page. I do not really understand how a page connects to the sidebar.

I have read the post:
http://get-simple.info/forums/showthread...t=sidebars

but I have absolutely no idea where to put the code:
<?php get_component((string) return_page_slug()); ?>

Any help would be very much appreciated thanks. Blush
Reply
#2
You want to put that code in the 'template.php' file of your theme (/themes/Theme-Folder/template.php) right in the place where the component with the proper sidebar needs to be.

This might be a good read.
Reply
#3
In your theme the file template.php will have the tag in it
PHP Code:
<?php get_component('sidebar'); ?>
which inserts the contents of the component sidebar into every page which uses that template.

An easy way to get a different sidebar on a new page is to copy and rename template.php and edit the new template to insert a different component.

That's fine for a small site but not particularly good practice because now if you want to make a change elsewhere in the template you need to change every template the same. Some snippets of code for using one template and multiple components by page slug are given here:
http://get-simple.info/wiki/components-d...n-the_page

In your case with three pages you could use the first snippet so that if you have pages with the slugs index, about, contact and components named peter, paul and mary you would replace <?php get_component(sidebar); ?> with
PHP Code:
<?php if (return_page_slug()=='index') {get_component('peter');}
 elseif (
return_page_slug()=='about') {get_component('paul');}
else {
get_component('mary');} ?>
which in English reads
Quote: if the page slug is index insert the component peter, otherwise if the page slug is about insert the component paul and otherwise just insert the component mary

The more complicated and complete example on the bottom of the wiki page prevents the code from failing when a component is missing and uses the page slug in the component name so that you can add pages later with specific components without editing the template.
Reply
#4
Thank you so much for your detailed instructions Timbow and your help Everyone - I finally understand not only how to have separate sidebars but also more about the structure of pages and components etc. I have knowledge of html & CSS but php is very challenging for me.

Apart from my own challenges I think GetSimple is the best CMS I've found so far and with Forum help such that I've just received - it can't get much better than this.

My thanks again....!
Reply
#5
(2014-05-02, 20:49:03)sher4 Wrote: Thank you so much for your detailed instructions Timbow ...
My thanks again....!

Actually I wrote the php wrong, I am correcting it now.

Done. I had left out the {}s .

I should keep my mouth shut really because I am still only learning this php stuff.
Reply
#6
If I'm not mistaken, this can be easier maintained by using the I18N Custom Fields plugin.
Reply
#7
In nearly any Webpage I use diffrent sidebars. Anything is dependend from the slug:

  1. Read the Slugname, extend it with e.g. "-sidebar"
  2. check if "slugname-sidebar.xml" exists
  3. If not, display a default one or nothing
  4. otherwise display the "slugname-sidebar.xml"

With the same technique you can set up different CSS files for any page if you want.

Of cource the custom fields plugin is more comfortable, but the slug-solution works out of the box.
Reply
#8
(2014-05-02, 20:16:30)Timbow Wrote: In your theme the file template.php will have the tag in it
PHP Code:
<?php get_component('sidebar'); ?>
 which inserts the contents of the component sidebar into every page which uses that template.

An easy way to get a different sidebar on a new page is to copy and rename template.php  and edit the new template to insert a different component.

That's fine for a small site but not particularly good practice because now if you want to make a change elsewhere in the template you need to change every template the same. Some snippets of code for using one template and multiple components by page slug are given here:
http://get-simple.info/wiki/components-d...n-the_page

In your case with three pages you could use the first snippet so that if you have pages with the slugs index, about, contact and components named peter, paul and mary you would replace <?php get_component(sidebar); ?> with
PHP Code:
<?php if (return_page_slug()=='index') {get_component('peter');}
 elseif (
return_page_slug()=='about') {get_component('paul');}
else {
get_component('mary');} ?>
which in English reads
Quote: if the page slug is index insert the component peter, otherwise if the page slug is about insert the component paul and otherwise just insert the component mary

The more complicated and complete example on the bottom of the wiki page prevents the code from failing when a component is missing and uses the page slug in the component name so that you can add pages later with specific components without editing the template.

This is one of the most helpful comments ever - thank you!  Big Grin
Reply




Users browsing this thread: 1 Guest(s)