更改从unctions.php获取的侧边栏get_side bar()

时间:2012-09-07 作者:supertrue

我将此代码保存在模板文件中sidebar-sidebar_a.php:

get_sidebar(\'sidebar_a\');
我想在提要栏中添加一个函数。php,如果满足某些条件,则会将其更改为:

get_sidebar(\'other\');
这样它就可以sidebar-other.php.

我原以为这会奏效,但似乎没有效果:

add_action(\'get_sidebar\', \'my_sidebar_logic\');

function my_sidebar_logic($sidebar) {
    // Load a different sidebar if the user is logged in
    if (is_user_logged_in()) {
        return \'other\';
    }

    return $sidebar;
}
我该怎么做才能达到我想要的效果?

4 个回复
SO网友:chrisguitarguy

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;
}

SO网友:Chip Bennett

只需将变量传递给get_sidebar():

// Determine sidebar
$sidebar = ( is_user_logged_in() ? \'other\' : \'sidebar_a\' );
// Get the sidebar
get_sidebar( $sidebar );

SO网友:kaiser

主题(Themes)

只需用最轻薄、最不具侵入性的逻辑来包装它:过滤器。

// In you theme: Calls the default sidebar
get_sidebar( apply_filters( \'wpse64492_sidebar\', \'\' ) );
然后您可以切换您的功能。php文件:

function wpse64492_sidebar_name( $name = \'\' )
{
    // Call sidebar-single.php on single view posts
    is_single() AND $name = \'single\';

    return $name;
}
add_filter( \'wpse64492_sidebar\', \'wpse64492_sidebar_name\' );

插件

如果您想从插件执行此操作,请使用以下操作:

<?php
/** Plugin Name: (#64492) Switch sidebars per request */
function wpse64492_sidebar( $name )
{
    // Conditional logic: Some other sidebar for page 2+ on paged archives
    if ( is_paged() )
    {
        // Avoid recurrsion: remove itself
        remove_filter( current_filter(), __FUNCTION__ );
        return get_sidebar( \'paged\' );
    }

    // Default return
    return $name;
}
add_action( \'get_sidebar\', \'wpse64492_sidebar\' );

SO网友:Wyck

您也可以在函数中调用实际的侧栏。

add_action(\'get_sidebar\', \'my_sidebar_logic\');

function my_sidebar_logic($sidebar) {
    // Load a different sidebar if the user is logged in
    if (is_user_logged_in()) {
          get_sidebar(\'other\');
    }   
}

结束

相关推荐

Quote rotator in the sidebar

我喜欢在帖子编辑器中设计一些帖子(加粗和链接)(简单而有趣),并将其归类为“引用”。然后在侧边栏中基于CSS显示它,随机旋转它,如每5秒显示1。你知道一个插件可以做到这一点吗?