获取侧边栏中的小部件数量

时间:2012-06-04 作者:nautilus7

如何获取特定侧栏上活动的小部件数量?这是否有一个核心功能?

我想根据侧边栏上显示的小部件的数量为每个小部件添加一个类。

谢谢

4 个回复
最合适的回答,由SO网友:nautilus7 整理而成

经过一些研究,并根据尤金·曼努伊洛夫的回答,我制作了一个函数,根据侧栏中设置的小部件数量,将自定义类添加到特定侧栏中的小部件中(“侧栏-底部”)。这将非常适合基于twitter引导的水平侧栏和主题,需要spanX类来调整元素的宽度。

function cosmos_bottom_sidebar_params($params) {

    $sidebar_id = $params[0][\'id\'];

    if ( $sidebar_id == \'sidebar-bottom\' ) {

        $total_widgets = wp_get_sidebars_widgets();
        $sidebar_widgets = count($total_widgets[$sidebar_id]);

        $params[0][\'before_widget\'] = str_replace(\'class="\', \'class="span\' . floor(12 / $sidebar_widgets) . \' \', $params[0][\'before_widget\']);
    }

    return $params;
}
add_filter(\'dynamic_sidebar_params\',\'cosmos_bottom_sidebar_params\');

SO网友:Nathaniel Schweinberg

此外,如果您希望对第一个和最后一个小部件进行操作,可以使用下面的代码。我把nautilus7的代码和MathSmath+durin的代码结合在一起http://wordpress.org/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets 要针对特定的侧栏,请根据侧栏中的小部件数量添加跨类,然后向组的第一个和最后一个小部件添加自定义类。

function cur_target_sidebar_add_classes_to_params($params) {

global $my_widget_num; // Global a counter array
$sidebar_id = $params[0][\'id\'];

if ( $sidebar_id == \'sidebar\' ) {

    $registered_widgets = wp_get_sidebars_widgets();
    if(!isset($registered_widgets[$sidebar_id]) || !is_array($registered_widgets[$sidebar_id])) { // Check if the current sidebar has no widgets
        return $params; // No widgets in this sidebar... bail early.
    }

    $number_of_widgets = count($registered_widgets[$sidebar_id]);
    $rounded_number_of_widgets = floor(12 / $number_of_widgets); //Rounds number of widgets down to a whole number

    if(!$my_widget_num) {// If the counter array doesn\'t exist, create it
        $my_widget_num = array();
    }

    if(isset($my_widget_num[$sidebar_id])) { // See if the counter array has an entry for this sidebar
        $my_widget_num[$sidebar_id] ++;
    } else { // If not, create it starting with 1
        $my_widget_num[$sidebar_id] = 1;
    }

    $classes = \'span\' . $rounded_number_of_widgets;

    if($my_widget_num[$sidebar_id] == 1) { // If this is the first widget
        $classes .= \' first-widget \';
    } elseif($my_widget_num[$sidebar_id] == count($registered_widgets[$sidebar_id])) { // If this is the last widget
        $classes .= \' last-widget \';
    }

    $params[0][\'before_widget\'] = preg_replace(\'/class=\\"/\', \'class="\' . $classes . \' \', $params[0][\'before_widget\'], 1);
    }

    return $params;
}
add_filter(\'dynamic_sidebar_params\',\'cur_target_sidebar_add_classes_to_params\');

SO网友:Eugene Manuilov
function wpse_54162_get_widgets_count( $sidebar_index ) {
    global $wp_registered_sidebars;

    $index = "sidebar-{$sidebar_index}";

    $sidebars_widgets = wp_get_sidebars_widgets();
    if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
        return 0;

    return count( (array) $sidebars_widgets[$index] );
}
SO网友:PCMShaper

让我们尝试以下代码

function sidebar_widget_init()
    {
        global $_wp_sidebars_widgets;
        if ( empty( $_wp_sidebars_widgets ) ) :
            $_wp_sidebars_widgets = get_option( \'sidebars_widgets\', array() );
        endif;

        $sidebars_widgets_count = $_wp_sidebars_widgets;
        $sidebar_count = count( $sidebars_widgets_count[ \'sidebar3\' ] );

        if($sidebar_count == 2){
            $sidebar_class = \'class_name1\';
        } elseif($sidebar_count == 3){
            $sidebar_class = \'class_name2\';
        } elseif($sidebar_count == 4){
            $sidebar_class = \'class_name3\';
        }
        register_sidebar(array( \'name\'          => __( \'Sidebar Footer\', \'templatemesh\' ),
                                \'id\'            => \'sidebar3\',
                                \'description\'   => __( \'Widgets in this area will be shown on footer Sidebar.\', \'templatemesh\' ),
                                    \'before_title\'  => \'<h3  class="widget_title">\',
                                    \'after_title\'   => \'</h3>\',
                                    \'before_widget\' => \'<div class="\'.$sidebar_class.\'"><div id="%1$s" class="widget %2$s" >\',
                                    \'after_widget\'  => \'</div></div>\'
                    )
        );
    }
    add_action(\'widgets_init\',\'sidebar_widget_init\');

结束

相关推荐

Adding content to sidebars

我正在设计一个三栏布局,根据主栏中的帖子/页面,所有三栏都有不同的内容。我如何才能改变和更新侧边栏内容,并使用以下内容在模板中显示它<?php the_content() ?> ? 由于侧边栏内容与帖子相关,我需要一种方法来引用帖子的右侧侧边栏内容。提前感谢诚挚的nav