I wanted so that my widget has the following structure:
<div class="widget-area"> <h4 class="widget-title">Foo Bar</h4> <div class="widget-content">Blablablabla blabla</div> </div>
so I registered my sidebar…
register_sidebar(array( 'name' => 'Sidebar Widget Area', 'id' => 'sidebar-widget-area', 'description' => 'Choose Widget(s) to display in this area.', 'before_widget' => '<div class="widget-area">', 'after_widget' => '</div></div>', 'before_title' => '<h4 class="widget-title">', 'after_title' => '</h4><div class="widget-content">' ));
It looked good at first… but things went awry whenever there’s a widget without title. The reason is kinda obvious, without the title, there will be an extra
</div> in the markup.
Is there any good method to achieve my desired markup? ( with/without a widget title )
NB: I’m sorry if the thread’s title is kinda different from the main question.
Nope, there is no way (thank I can think of) to do this cleanly.
Handling of output and related parameters is up to the each wdiget’s individual
widget() method. There is no generic way to manipulate it.
You could maybe get away with filtering widget save or display instances to force it to always have title (like a single space string for example), but this would be meh semantically and likely visually.