Get sidebar是一个非常薄的包装locate_template
, 它只在当前子主题和父主题目录中搜索给定的侧栏。
get_sidebar
在里面wp-includes/general-template.php
:
$templates = array();
if ( isset($name) )
$templates[] = "sidebar-{$name}.php";
$templates[] = \'sidebar.php\';
// Backward compat code will be removed in a future release
if (\'\' == locate_template($templates, true))
load_template( ABSPATH . WPINC . \'/theme-compat/sidebar.php\');
}
和locate_template
(英寸wp-includes/template.php
):
<?php
/**
* Retrieve the name of the highest priority template file that exists.
*
* Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
* inherit from a parent theme can just overload one file.
*
* @since 2.7.0
*
* @param string|array $template_names Template file(s) to search for, in order.
* @param bool $load If true the template file will be loaded if it is found.
* @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
* @return string The template filename if one is located.
*/
function locate_template($template_names, $load = false, $require_once = true ) {
$located = \'\';
foreach ( (array) $template_names as $template_name ) {
if ( !$template_name )
continue;
if ( file_exists(STYLESHEETPATH . \'/\' . $template_name)) {
$located = STYLESHEETPATH . \'/\' . $template_name;
break;
} else if ( file_exists(TEMPLATEPATH . \'/\' . $template_name) ) {
$located = TEMPLATEPATH . \'/\' . $template_name;
break;
}
}
if ( $load && \'\' != $located )
load_template( $located, $require_once );
return $located;
}
你上钩的原因
get_sidebar
不起作用是因为这是一种行为。它只是开火。它不允许您更改结果。不管你们的钩状动作如何,你们的侧边栏仍然会被包括在内。
这为您提供了一些选择。
1. Pass a variable into get_sidebar
Chip Bennett的解决方案:将变量传递到
get_sidebar
作用不错。
2. Write Your Own Sidebar Logic
并用它代替
get_sidebar
. 一个简单的例子:
<?php
function mytheme_sidebar($name)
{
$name = apply_filters(\'mytheme_sidebar\', $name);
get_sidebar($name);
}
然后使用您自己的过滤器修改侧栏
<?php
add_filter(\'mytheme_sidebar\', \'mytheme_custom_sidebar\');
function mytheme_custom_sidebar($name)
{
return is_user_logged_in() ? \'loggedin\' : $name;
}
这将是最灵活的。如果它是一个公共主题,最终用户将能够自定义内容。如果是为了客户,当不可避免地要求你提供更多的东西时,这很容易做到。
一
<?php
function mytheme_sidebar($name)
{
$name = apply_filters(\'mytheme_sidebar\', $name);
get_sidebar($name);
}
dynamic_sidebar
是完成工作的地方。如果查看该函数内部,最有趣的一行是:
$sidebars_widgets = wp_get_sidebars_widgets();
wp_get_sidebars_widgets
获取站点上所有侧栏的所有当前注册的小部件。<?php
/**
* Retrieve full list of sidebars and their widgets.
*
* Will upgrade sidebar widget list, if needed. Will also save updated list, if
* needed.
*
* @since 2.2.0
* @access private
*
* @param bool $deprecated Not used (deprecated).
* @return array Upgraded list of widgets to version 3 array format when called from the admin.
*/
function wp_get_sidebars_widgets($deprecated = true) {
if ( $deprecated !== true )
_deprecated_argument( __FUNCTION__, \'2.8.1\' );
global $wp_registered_widgets, $_wp_sidebars_widgets, $sidebars_widgets;
// If loading from front page, consult $_wp_sidebars_widgets rather than options
// to see if wp_convert_widget_settings() has made manipulations in memory.
if ( !is_admin() ) {
if ( empty($_wp_sidebars_widgets) )
$_wp_sidebars_widgets = get_option(\'sidebars_widgets\', array());
$sidebars_widgets = $_wp_sidebars_widgets;
} else {
$sidebars_widgets = get_option(\'sidebars_widgets\', array());
}
if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets[\'array_version\']) )
unset($sidebars_widgets[\'array_version\']);
$sidebars_widgets = apply_filters(\'sidebars_widgets\', $sidebars_widgets);
return $sidebars_widgets;
}
关键在于:apply_filters(\'sidebars_widget\', ...)
. 钱仅为登录用户注册辅助侧栏:
<?php
add_action(\'widgets_init\', \'wpse64492_register\');
function wpse64492_register()
{
register_sidebar(array(
\'name\' => __(\'Logged In Sidebar\', \'wpse64492\'),
\'id\' => \'logged-in\'
));
}
然后钩住sidebars_widgets
并将普通侧栏替换为登录版本。<?php
add_filter(\'sidebars_widgets\', \'wpse64492_switch\');
function wpse64492_switch($widgets)
{
if(is_admin())
return $widgets;
$key = \'sidebar-1\'; // the sidebar you want to change!
if(isset($widgets[$key]) && is_user_logged_in() && isset($widgets[\'logged-in\']))
$widgets[$key] = $widgets[\'logged-in\'];
return $widgets;
}