Tag Archives: theme

Replacing primary sidebar with the secondary sidebar

On my pages, viagra usa pharmacy I have selected a two column format from my theme’s options. The primary sidebar is displayed to the right of the main content. I want to replace that with the secondary sidebar.( I don’t want to effect Featured Items, individual posts, or category pages. More on that later.)
Starting with my theme’s page.php, I find a call to php get_sidebar()on line 19. From the wordpress codex:

Includes the sidebar.php template file from your current theme’s directory.

If a name ($name) is specified then a specialized sidebar sidebar-{name}.php will be included. If sidebar-{name}.php does not exist, then it will fallback to loading sidebar.php.

If the theme contains no sidebar.php file then the sidebar from the default theme wp-includes/theme-compat/sidebar.php will be included.

That brings us to sidebar.php.

First I made a copy of my theme’s sidebar.php, and placed it in in my child theme’s folder. We’ll make any edits to this new file.

Lets take a look at what’s in the theme’s sidebar.php. In line 2, we have a reference to a global variable:
global $sds_theme_options;
This gives us access to the global variable $sds_theme_options from within our function. Line 5 has the first reference to this global variable.
if ( ! isset( $sds_theme_options['body_class'] ) || ( ! empty( $sds_theme_options['body_class'] ) && $sds_theme_options['body_class'] !== 'cols-1' ) ) :
a or (b and c). If body class is not set in the theme options, or the body class is not empty, and the body class is not set as ‘cols-1′, meaning the page has more than one column, then show the primary sidebar.

The next if statement is similar. It controls whether the secondary sidebar is shown or not.
if ( isset( $sds_theme_options['body_class'] ) && ! empty( $sds_theme_options['body_class'] ) && strpos( $sds_theme_options['body_class'], 'cols-3' ) !== false ) :
If body class is set in theme options, and the body class is not empty, and the string ‘cols-3′ exists in the $sds_theme_options variable, then we know we have a layout that calls for the secondary sidebar.

With this setup, as long as the body class is not ‘cols-1′, then the primary sidebar is shown to the right(as is set in the theme options for pages). As long as ‘cols-3′ is part of the body class, the primary sidebar is moves to the left, and the secondary sidebar is shown to the right. The only time the secondary sidebar is shown is if the primary sidebar is also there. What we want is to have the secondary sidebar show in the primary sidebar’s position if there are two columns, and have the primary and secondary sidebars show in their normal positions if there are three columns.

I decided to add an additional if and else to the top of my child theme’s sidebar.php. If body class is set, and body class is not empty, and body class contains the text ‘cols-2′, then put the secondary sidebar in the primary sidebar’s position. else, do what the function used to do. this way I’m only affecting the two column pages. Remember, sidebar.php gets used in every type of page, post, category, etc.

Here is my final sidebar.php: