在小部件标题之后添加div以包装小部件内容

时间:2012-12-03 作者:MBraiN

我试图在动态侧边栏中向小部件的内容添加div。

这是注册码;

register_sidebar(array(
        \'name\' => "Sidebar1",
        \'id\' => \'home-sidebar-1\',
        \'before_widget\' => \'<div class="sidebar-box">\',
        \'after_widget\' => \'</div>\',
        \'before_title\' => \'<div class="title">\',
        \'after_title\' => \'</div>\',
    ));
但是这个代码会导致这样的结果;

<div class="sidebar-box">
    <div class="title">{WIDGET TITLE}</div>
     {WIDGET CONTENT}
</div>
这就是我要做的;

<div class="sidebar-box">
    <div class="title">{WIDGET TITLE}</div>
    <div class="content">
           {WIDGET CONTENT}
    </div> 
</div>
如何做到这一点?

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

除了Toscho的答案之外,您还需要以下内容才能获得强健的解决方案:

// if no title then add widget content wrapper to before widget
add_filter( \'dynamic_sidebar_params\', \'check_sidebar_params\' );
function check_sidebar_params( $params ) {
    global $wp_registered_widgets;

    $settings_getter = $wp_registered_widgets[ $params[0][\'widget_id\'] ][\'callback\'][0];
    $settings = $settings_getter->get_settings();
    $settings = $settings[ $params[1][\'number\'] ];

    if ( $params[0][ \'after_widget\' ] == \'</div></div>\' && isset( $settings[ \'title\' ] ) && empty( $settings[ \'title\' ] ) )
        $params[0][ \'before_widget\' ] .= \'<div class="content">\';

    return $params;
}
根据托肖的回答:

register_sidebar(array(
    \'name\' => "Sidebar1",
    \'id\' => \'home-sidebar-1\',
    \'before_widget\' => \'<div class="sidebar-box">\',
    \'after_widget\' => \'</div></div>\',
    \'before_title\' => \'<div class="title">\',
    \'after_title\' => \'</div><div class="content">\',
));
其作用是:

检查以2结尾的小部件的已注册侧栏的设置<div>s检查是否没有标题如果没有,则修改before_widget 输出以显示打开的小部件内容div,您可以使用此方法以其他方式根据小部件实例的设置更改侧栏参数。

SO网友:rabmcnab

我喜欢“tosco”和“sanchothefat answers”这两种答案的想法,但我一直在为这种组合而挣扎,因为empty( $settings[ \'title\' ] 可能确实返回true如果未设置任何标题,小部件本身可能会输出默认标题。然后将此标题包装在before_titleafter_title 标记,然后再次分页符。一些默认小部件就是这样。

更容易坚持标准小部件注册参数;

register_sidebar(array(
    \'id\' => \'sidebar1\',
    \'name\' => __( \'Sidebar 1\', \'bonestheme\' ),
    \'description\' => __( \'The first (primary) sidebar.\', \'bonestheme\' ),
    \'before_widget\' => \'<div class="block">\',
    \'after_widget\' => \'</div>\',
    \'before_title\' => \'<div class="block-title">\',
    \'after_title\' => \'</div>\',
));
然后根据Marventus的回答在此处添加过滤动作;

http://wordpress.org/support/topic/add-html-wrapper-around-widget-content

function widget_content_wrap($content) {
    $content = \'<div class="block-content">\'.$content.\'</div>\';
    return $content;
}
add_filter(\'widget_text\', \'widget_content_wrap\');

SO网友:fuxia

添加第二个div 标题参数:

register_sidebar(array(
        \'name\' => "Sidebar1",
        \'id\' => \'home-sidebar-1\',
        \'before_widget\' => \'<div class="sidebar-box">\',
        \'after_widget\' => \'</div></div>\',
        \'before_title\' => \'<div class="title">\',
        \'after_title\' => \'</div><div class="content">\',
    ));

SO网友:cjbj

以下是实现这一目标的方法:

register_sidebar(array(
        \'name\' => "Sidebar1",
        \'id\' => \'home-sidebar-1\',
        \'before_widget\' => \'<div class="sidebar-box"><div class="content">\',
        \'after_widget\' => \'</div></div>\',
        \'before_title\' => \'</div><div class="title">\',
        \'after_title\' => \'</div><div class="content">\',
    ))
如果没有标题,您将获得:

<div class="sidebar-box"><div class="content">
{CONTENT}
</div></div>
当有标题时,您将获得:

<div class="sidebar-box"><div class="content">
</div><div class="title">
{TITLE}
<div class="content">
{CONTENT}
</div></div>
要确保后一种情况下不显示第一个空内容div,请将其添加到css中:

.sidebar-box .content:empty {display:none !important;}

SO网友:Spencer

在查看了前面的答案后,我想我解决了sanchothefat的答案所确定的问题Cmorales。按如下方式定义小部件:

register_sidebar( array(
    \'name\' => \'Sidebar\',
    \'id\' => \'sidebar\',
    \'before_widget\' => \'<div class="panel panel-default %2$s" id="%1$s">\',
    \'after_widget\' => \'</div>\',
    \'before_title\' => \'\',
    \'after_title\' => \'\'
) );
重要的是before_titleafter_title 默认值将被删除。经过一些尝试和错误,我注意到显示默认标题的第一个过滤器是widget_title. 然后使用widget_title 像这样过滤:

function widget_title( $title ) {
    if ( $title )
        $title = \'<div class="panel-heading"><h3 class="panel-title">\' . $title . \'</h3></div>\';
    $title .= \'<div class="panel-body">\';
    return $title;
}
基本上,<div class="panel-heading"><h3 class="panel-title"> 是我的before_title</h3></div> 是我的after_title. 最后,使用dynamic_sidebar_params 要为内容包装预先添加结束div,请执行以下操作:

function dynamic_sidebar_params( $params ) {
    $params[0][\'after_widget\'] = \'</div>\' . $params[0][\'after_widget\'];
    return $params;
}
这并不漂亮,但很管用。

SO网友:Razon Komar Pal

我只是稍微修改一下代码,这样就不需要碰register_sidebar. 以常规方式注册侧栏:

register_sidebar(array(
    \'name\' => "Sidebar1",
    \'id\' => \'home-sidebar-1\',
    \'before_widget\' => \'<div class="sidebar-box">\',
    \'after_widget\' => \'</div>\',
    \'before_title\' => \'<div class="title">\',
    \'after_title\' => \'</div>\',
));
下面是Sanchothefat解决方案的修改代码:

/**
 * If no title given then add widget-content wrapper to before_widget
 * If title given then add content wrapper to before_widget
*/
add_filter(\'dynamic_sidebar_params\', \'check_widget_template\');
function check_widget_template($params){
    global $wp_registered_widgets;

    $settings_obj = $wp_registered_widgets[$params[0][\'widget_id\']][\'callback\'][0];
    $the_settings = $settings_obj->get_settings();
    $the_settings = $the_settings[$params[1][\'number\']];

    if (isset($params[0][\'id\']) && $params[0][\'id\'] == \'home-sidebar-1\') {
        if (!isset($the_settings[\'title\']) && empty($the_settings[\'title\'])) {
            $params[0][\'before_widget\'] .= \'<div class="widget-inner">\';
            $params[0][\'after_widget\']  .= \'</div>\';
        } else {
            $params[0][\'after_widget\']  .= \'</div>\';
            $params[0][\'after_title\']   .= \'<div class="widget-inner">\';
        }
    }

    return $params;
}

SO网友:Azragh

如评论中所述,一些核心小部件添加了自己的标题,然后用content div包装两次。要删除它们,只需返回一个空字符串即可。唯一的缺点是您必须手动输入所有标题。

function remove_default_widget_title( $title, $instance = [], $id = \'\' ) {
    if ( isset($instance[\'title\']) && trim($instance[\'title\']) == \'\' ) {
        return \'\';
    }
    return $title;
};
add_filter( \'widget_title\', \'remove_default_widget_title\', 10, 3 );
如果我遗漏了什么,请纠正我,但我认为这很可靠。

结束

相关推荐

Widgets in PHP files?

有没有可能让Wordpress上的每个小部件都以不同的方式运行。php文件我有一个包含12个元素的页面,我想让每个元素都成为一个小部件,以便以后更容易管理/编辑它们,但如果它来自php页面,而不是来自管理面板上的代码块,则会更好。