I’m trying to add a div to a widget’s content in my dynamic sidebar.
Here is the register code;
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>',
));
But this code causes like this;
<div class="sidebar-box">
<div class="title">{WIDGET TITLE}</div>
{WIDGET CONTENT}
</div>
Here is what I am trying to do;
<div class="sidebar-box">
<div class="title">{WIDGET TITLE}</div>
<div class="content">
{WIDGET CONTENT}
</div>
</div>
How it can be done?
In addition to Toscho’s answer here’s what you need for a robust solution:
From Toscho’s answer:
What this does is:
<div>
sbefore_widget
output to show the opening widget content divYou could use this approach to change the sidebar arguments in other ways to based on the widget instance’s settings.
I liked the idea of both @tosco and @sanchothefat answers – however I have struggled with that combination because whilst
empty( $settings[ 'title' ]
might indeed return true the widget itself might output a default title if none is set. This title is then wrapped in thebefore_title
andafter_title
markup and again the page breaks. That’s the case with some of the default widgets anyway.Easier to just stick to standard widget registration parameters;
and then add a filter action as per Marventus’ answer here;
http://wordpress.org/support/topic/add-html-wrapper-around-widget-content
Add the second
div
to the title parameter:Here’s what you do to achieve this:
When there is no title you will get:
When there is a title you will get:
To make sure the first empty content-div doesn’t show in the latter case you add this to your css:
After looking at the previous answers, I think I fixed the problem Cmorales identified with sanchothefat’s answer. Define your widget as follows:
It’s important that the
before_title
andafter_title
defaults are removed. After some trial and error I noted that the first filter that showed a default title waswidget_title
. Then use thewidget_title
filter like so:Basically,
<div class="panel-heading"><h3 class="panel-title">
is mybefore_title
and</h3></div>
is myafter_title
. Finally, usedynamic_sidebar_params
to prepend a closing div for our content wrap:It isn’t pretty, but it works.
I just slightly change the code so that don’t need to touch register_sidebar. Register a sidebar with regular way:
And below is modified code From Sanchothefat’s Solution:
As mentioned in the comments, some core widgets add their own title which then gets wrapped twice with the content div. To remove them, you could simply return an empty string. The only downside is that you have to enter all the titles manually.
Correct me if I’m missing something here, but I think that’s quite solid.