你很幸运。我其实是在找借口在我的一个项目上这样做。我终于花时间把这个想法添加到我的一个主题中
你可以接受这个想法并付诸实践。这是一个非常简单的例子,但它做了您希望它做的事情。
这不包括主题选项。这假设您已经以某种方式管理了主题设置(听起来像是这样)。
Step 1 - Tell WordPress about "Widget Areas" supported by your theme.
if ( ! function_exists( \'mbe_theme_register_widget_areas\' ) ) {
/**
* This function tells WordPress that your theme supports up to 4 Widget Areas:
* 1.) Header Column 1
* 2.) Header Column 2
* 3.) Header Column 3
* 4.) Header Column 4
*
* Allows Widgets to be placed in various places within the theme header.
*
* @author Michael Ecklund
* @author_url http://www.michaelbrentecklund.com/
*
* @return bool
*/
function mbe_theme_register_widget_areas() {
$widget_areas = array();
$widget_areas[] = array(
\'name\' => \'Header Column 1\',
\'id\' => \'mbe-theme-widget-area-header-column-1\'
);
$widget_areas[] = array(
\'name\' => \'Header Column 2\',
\'id\' => \'mbe-theme-widget-area-header-column-2\'
);
$widget_areas[] = array(
\'name\' => \'Header Column 3\',
\'id\' => \'mbe-theme-widget-area-header-column-3\'
);
$widget_areas[] = array(
\'name\' => \'Header Column 4\',
\'id\' => \'mbe-theme-widget-area-header-column-4\'
);
foreach ( $widget_areas as $widget_area ) {
$default_args = array(
\'before_widget\' => \'<div id="%1$s" class="widget mbe-theme-widget %2$s">\',
\'after_widget\' => \'</div><!-- end .mbe-theme-widget -->\',
\'before_title\' => \'<div class="mbe-theme-widget-title">\',
\'after_title\' => \'</div><!-- end .mbe-theme-widget-title -->\'
);
$args = wp_parse_args( $widget_area, $default_args );
register_sidebar( $args );
}
return true;
}
add_action( \'widgets_init\', \'mbe_theme_register_widget_areas\' );
}
以上代码应该粘贴到当前活动的WordPress主题中
functions.php
文件
Step 2 - Create the template files for each type of header.
在本例中,您需要在当前活动WordPress主题的根目录中创建4个新模板文件:
header-column-1.php
header-column-2.php
header-column-3.php
header-column-4.php
Step 3 - Make each new header template file widget ready.
在本例中,我将演示一个4列标题模板(
header-column-4.php
):
<?php
# BoF Header Column 1
echo \'<section class="col-lg-3">\' . PHP_EOL;
if ( is_active_sidebar( \'mbe-theme-widget-area-header-column-1\' ) ) {
// Display Widgets
dynamic_sidebar( \'mbe-theme-widget-area-header-column-1\' );
} else {
// No Widgets, display default fallback markup.
echo \'Default Column #1\' . PHP_EOL;
}
echo \'</section><!-- end .col-lg-3 -->\' . PHP_EOL;
# EoF Header Column 1
# BoF Header Column 2
echo \'<section class="col-lg-3">\' . PHP_EOL;
if ( is_active_sidebar( \'mbe-theme-widget-area-header-column-2\' ) ) {
// Display Widgets
dynamic_sidebar( \'mbe-theme-widget-area-header-column-2\' );
} else {
// No Widgets, display default fallback markup.
echo \'Default Column #2\' . PHP_EOL;
}
echo \'</section><!-- end .col-lg-3 -->\' . PHP_EOL;
# EoF Header Column 2
# BoF Header Column 3
echo \'<section class="col-lg-3">\' . PHP_EOL;
if ( is_active_sidebar( \'mbe-theme-widget-area-header-column-3\' ) ) {
// Display Widgets
dynamic_sidebar( \'mbe-theme-widget-area-header-column-3\' );
} else {
// No Widgets, display default fallback markup.
echo \'Default Column #3\' . PHP_EOL;
}
echo \'</section><!-- end .col-lg-3 -->\' . PHP_EOL;
# EoF Header Column 3
# BoF Header Column 4
echo \'<section class="col-lg-3">\' . PHP_EOL;
if ( is_active_sidebar( \'mbe-theme-widget-area-header-column-4\' ) ) {
// Display Widgets
dynamic_sidebar( \'mbe-theme-widget-area-header-column-4\' );
} else {
// No Widgets, display default fallback markup.
echo \'Default Column #4\' . PHP_EOL;
}
echo \'</section><!-- end .col-lg-3 -->\' . PHP_EOL;
# EoF Header Column 4
echo \'<div class="clearfix"></div>\' . PHP_EOL;
?>
Step 4 - Edit header.php
in your currently active theme to activate dynamic header output.
<?php
echo \'<section class="container">\' . PHP_EOL;
echo \'<section class="row">\' . PHP_EOL;
$header_template = get_option( \'mbe-theme-header-template\' ); // Retrieve theme option configured by user.
if ( ! $header_template ) {
$header_template = \'column-3\'; // Fallback: Set a default header template.
}
get_template_part( \'header\', $header_template );// Load the header template.
echo \'</section><!-- end .row -->\' . PHP_EOL;
echo \'</section><!-- end .container -->\' . PHP_EOL;
?>