Displaying widget title inside the before_widget variable
-
Hello,
I’m new to the wordpress community. I’m building a custom wordpress theme for my website and I need some help with its costumization.
The thing I am struggling to do is to display the widget title in the “before_widged” variable. Why I want to do this ? Well I will try to explain:I have wrapped the widget contents in a number of <div> tags. These divs are forming a content box. Its structure is as follows:
<div class="box_container_mid"> <div class="boxheadl"></div><div class="boxheadr"></div> <div class="boxcontent"> <div class="cont1"></div> <div class="cont2"></div> <div class="cont3"</div> </div> <div class="boxfooter"> <div class="boxfl"></div> <div class="boxfr"></div> </div> </div>the way I make my widgets appear in that “box” is by changing some stuff in functions.php in my theme folder:
// Define Sidebar Widget Area 1 register_sidebar(array( 'name' => __('Widget Area 1', 'mytheme'), 'description' => __('Description for this widget-area...', 'mytheme'), 'id' => 'widget-area-1', 'before_widget' => '<div class="box_container_mid"><div class="boxheadl"></div><div class="boxheadr"></div><div class="boxcontent"><div class="cont1"></div><div class="cont2"></div><div class="cont3">', 'after_widget' => '</div></div><div class="boxfooter"><div class="boxfl"></div><div class="boxfr"></div></div></div><div class="leftspacer"></div>', 'before_title' => '<h3>', 'after_title' => '</h3>' ));So what I want to do is put the widget title between the <div class=”boxeadl”> and its closing </div> tag, thats where I need the title do appear. How do I do that ?
I also had a look at widgets.php and default_widget.php files tried to think of something myself, but with no success. I will be extremely thankfull if someone helps me with that.
-
I’m in a bit of hurry and I can’t test it, but this should do the job:
'before_widget' => '<div class="box_container_mid">', 'after_widget' => 'the_rest_of_the_divs', 'before_title' => '<div class="boxheadl">', 'after_title' => '</div><div class="boxheadr"></div>'Thanks alot,
I’ve actually did it. I just had to figure out which of the tags is getting rendered first (not so complicated):
1. before_widget
2. before_title
3. after_title
4. after_widgetso I have something like this now:
// Define Sidebar Widget Area 1 register_sidebar(array( 'name' => __('Widget Area 1', 'mytheme'), 'description' => __('Description for this widget-area...', 'mytheme'), 'id' => 'widget-area-1', 'before_widget' => '<div id="box_container_mid">', 'after_widget' => '</div></div><div class="boxfooter"><div class="boxfl"></div><div class="boxfr"></div></div></div><div class="leftspacer"></div>', 'before_title' => '<div class="boxheadl">', 'after_title' => '</div><div class="boxheadr"></div><div class="boxcontent"><div class="cont1"></div><div class="cont2"></div><div class="cont3">' ));I’m extremely thankfull for your advice, I just had to make my brain move a bit and with your help its done 🙂
Edit:
I’m having some trouble now when no title is defined. The before_title stuff is not displayed if there is no title -.-Maybe you can try with simplifying the css and reduce the number of needed classes. Or you can try using some of the HTML special characters as title, like
 .
The topic ‘Displaying widget title inside the before_widget variable’ is closed to new replies.