WordPress

Pieter Van Looy
Pieter Van Looy
7,762 Points

Don't we have to print out our sidebar's in the sidebar template files, but putting the code get_sidebar() ?

I don't see the code to print out our sidebar's. How does WP knows which sidebar to print and where to print?

1 Answer

Liam Clarke
MOD
Liam Clarke
Treehouse Moderator 19,240 Points

Hi Pieter

the get_sidebar function gets the file sidebar.php by default.

If you had multiple sidebars you would name them with the prefix of sidebar and then pass in the parameter of the sidebar name.

for example:

in my project i have 2 sidebars.

one is the default and one is for the search page.

so i add 2 files.

sidebar.php

sidebar-search.php // This can be called anything as long as it starts with sidebar.
<?php
// in my index.php


get_sidebar(); // This will just get the default sidebar
<?php
// In my search.php

get_sidebar( 'search' ); // now wordpress knows to look for a file that starts with sidebar and ends in search, seperated by a hyphen ( - )

Hope this helps

Liam