更简单(&A);优雅(更高的可维护性):
<?php
// Your sidebar should have the wp_meta action hook
wp_meta();
// in ex. your functions.php
function my_sidebar_content() {
// "About" Page
if ( is_page(\'about\') ) {
// If some widget is added via Admin > Design > Widgets
if ( is_active_sidebar( \'widgets-sidebar-default\' ) ) {
// Display Widgets
dynamic_sidebar( \'widgets-sidebar-default\' );
}
// Default Content before Widgets were added
else {
_e(\'default static content\', TEXTDOMAIN);
}
}
// "Links" Page
elseif ( is_page(\'links\') ) {
if ( is_active_sidebar( \'widgets-sidebar-links\' ) ) {
dynamic_sidebar( \'widgets-sidebar-links\' );
}
else {
_e(\'default static content\', TEXTDOMAIN);
}
}
}
add_action( \'wp_meta\', \'my_sidebar_content\', 10 );
# ===================================================
// OR:
wp_meta();
// functions.php
function load_my_sidebars() {
// "About" Page
if ( is_page(\'about\') ) {
get_template_part( \'sidebar_content\', \'default\' );
}
// "Links" Page
elseif ( is_page(\'links\') ) {
get_template_part( \'sidebar_content\', \'links\' );
}
}
add_action( \'wp_meta\', \'load_my_sidebars\', 10 );
// in sidebar_content-default.php
// If some widget is added via Admin > Design > Widgets
// You can add any static content right here before the widgets
if ( is_active_sidebar( \'widgets-sidebar-default\' ) ) {
// Display Widgets
dynamic_sidebar( \'widgets-sidebar-default\' );
}
// Default Content before Widgets were added
else {
_e(\'default static content\', TEXTDOMAIN);
}
// You can add any static content right here after the widgets
// in sidebar_content-links.php
if ( is_active_sidebar( \'widgets-sidebar-links\' ) ) {
dynamic_sidebar( \'widgets-sidebar-links\' );
}
else {
_e(\'default static content\', TEXTDOMAIN);
}
?>