-
Notifications
You must be signed in to change notification settings - Fork 41
Controlling theme layout
Thematic ships with four different layouts: right sidebar, left sidebar, three columns and full width. The sidebar is removed in the full-width layout. The user can choose the main theme layout by selecting a radio button in the layout section of the Theme Customizer. However, child themes have full control over when and where a layout will be used with the help of some filters. Child themes can also decide to remove the layout section from the Customizer if they want complete control over the theme layout.
This is the order of precedence when deciding which layout is used on a page:
- Pages using the full-width page template will have a full width layout
- Any layout set by the child theme using the
thematic_current_theme_layout
filter - The default layout chosen by the user in the Theme Customizer
- The fallback default layout. Out of the box, this is Right Sidebar
The full width page template will use the full width layout, no matter what the site layout is. If your child theme is adding page templates and want to use a certain layout on a certain page template, use the filter thematic_current_theme_layout
.
Child themes can use the thematic_current_theme_layout
filter to decide the layout of the current page. This allows specifying theme layout on a per-page or per-section level. Basically anything that can be targeted using the standard WordPress conditionals. You can be as specific or as generic as you want. The layout slug has to be one of the available layouts, any other string will be ignored and the default layout will be used instead.
function mychild_change_layout( $layout ) {
if ( is_page_template( 'my-child-template' ) ) {
$layout = 'left-sidebar';
} elseif ( is_front_page() ) {
$layout = 'three-columns';
}
return $layout;
}
add_filter( 'thematic_current_theme_layout', 'mychild_change_layout' );
Note that using this filter will override whatever is set by the user in the Theme Customizer. This means that if you use this filter to define a layout without any conditional, that layout is what will be used and changing the Theme Customizer setting will have no effect whatsoever. In that case, please remove the layout section from the Customizer. It is easy to do and will reduce confusion for the user.
The user can change the main theme layout by selecting a radio button in the Theme Customizer layout section. This will set the main layout of the whole site, not on a per page or per section basis. The chosen layout is stored in the theme options.
Child themes have control over the available options using the filter thematic_available_theme_layouts
. See the page about available layouts for examples on how to use that.
If a child theme wants complete control over how and when each layout is used, it can remove the theme support for the customizer layout section:
function mychild_theme_supports() {
remove_theme_support('thematic_customizer_layout');
}
add_action( 'thematic_child_init', 'mychild_theme_supports');
It is important that the call to remove_theme_support()
is hooked to the thematic_child_init
action. Simply placing it in the functions.php file will not work because of the order that the files are included in WordPress.
Removing this section means that the layout will solely be determined by the theme and it's filters, the user has no easy way to change it. This might be desired in some cases, depending on the complexity of the theme and how tailor made it is for specific circumstances.
The default layout out of the box is Right Sidebar. This can be changed using the filter thematic_default_theme_layout
. This mainly affects which layout is used upon first installation, before the user has chosen anything else. Note that the layout slug has to be one of the available layouts, any invalid string will be ignored and default to right-sidebar
function mytheme_alter_default_layout( $layout ) {
return 'left-sidebar';
}
add_filter( 'thematic_default_theme_layout', 'mytheme_alter_default_layout' );