删除 WordPress 侧边栏小部件中的空标题
我已经注册了一个 WordPress 侧边栏,如下所示:
register_sidebar( array(
'name' => __( 'First Sidebar', 'theme_name' ),
'id' => 'primary-widget-area',
'description' => __( 'The primary widget area', 'theme_name' ),
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
问题是,当标题为空时,h3 仍然会被渲染。当标题留空时有没有办法删除这些?
I have registered a wordpress sidebar like so:
register_sidebar( array(
'name' => __( 'First Sidebar', 'theme_name' ),
'id' => 'primary-widget-area',
'description' => __( 'The primary widget area', 'theme_name' ),
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
The problem is that when the title is empty, the h3's still get rendered. Is there a way to remove these when the title is left blank?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这只是马克回答的一个小补充。如果标题为空,默认日历小部件将使用
,因此它仍然显示空标题。我通过将其添加到主题的
functions.php
中来解决这个问题:将 'foo' 更改为适当的内容。
This is just a small addition to Mark's answer. The default calendar widget uses
if the title is empty, so it still displays an empty header. I worked round that by adding this to my theme's
functions.php
:Changing 'foo' to something appropriate.
打印
before_title
和after_title
是由小部件自身在函数widget( $args, $instance )
中完成的。所有默认的 WordPress 3.1 小部件都会在解析before_title
和after_title
之前检查标题是否为空,但我猜您正在使用主题或插件中的自定义小部件,在这种情况下,您必须调整widget( $args, $instance )
代码。Printing the
before_title
andafter_title
is something that is done in the functionwidget( $args, $instance )
by the widget self. All of the default wordpress 3.1 widgets check if the title is empty before parsingbefore_title
andafter_title
, but I guess you're using a custom widget from a theme or plugin, in that case you'll have to adjust thewidget( $args, $instance )
code.我想感谢丹尼尔·詹姆斯的片段——这太漂亮了。我做了一个小改动,替换了 与 !no_display,然后将 !no_display 添加到前端小部件的标题中。这让我的用户清楚地知道这是一个在函数中引用的钩子(不要与看似空的小部件标题混淆)。
Wanted to thank Daniel James for his snippet - this is beautiful. I made a small change where I replaced with !no_display, then added !no_display to the title of my widgets in the front-end. This made it clear to my users that it was a hook to be referenced in a function (and not to be confused with a seemingly empty widget title).
编辑模板并检查标题是否存在。如果未设置标题,则不打印 h3。
Edit the template and check for the existence of a title. If no title is set do not print the h3.
注册两个侧边栏,除了
'before_title'
和'after_title'
值相同。检查标题,然后相应地调用其中之一。Register two sidebars, identical but for the
'before_title'
and'after_title'
values. Check for a title, and then call one or the other accordingly.