Widgets är en så integrerad del av WordPress teman att det är svårt att föreställa sig ett WordPress tema utan widgets. Widgetar är körbara skript som du helt enkelt kan drag and drop i dina sidebar eller något annat widget-klart area i ditt theme. Många av våra läsare använder widgetar för att add to custom element till deras sidebar. Men den här artikeln är för de nyfikna användare som vill lära sig hur man addar dynamiska widgetklara sidebar eller widgetklara areas i WordPress teman.
Registrera sidebar eller widget-redo areas i WordPress
Det första du behöver göra är att registrera din sidebar eller widgetar för ditt theme. You can register multiple sidebars and widget ready areas. Copy and paste den här koden i ditt temas functions.php-fil
function wpb_widgets_init() { register_sidebar( array( 'name' => __( 'Main Sidebar', 'wpb' ), 'id' => 'sidebar-1', 'description' => __( 'The main sidebar appears on the right on each page except the front page template', 'wpb' ), 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) ); register_sidebar( array( 'name' =>__( 'Front page sidebar', 'wpb'), 'id' => 'sidebar-2', 'description' => __( 'Appears on the static front page template', 'wpb' ), 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) ); } add_action( 'widgets_init', 'wpb_widgets_init' );
I den här koden har vi registrerat två sidebars. Vi har gett dem namn och beskrivningar för att identifiera dem på vyn Widgets. Parametern description kan användas för att tala om för användare var denna sidebar appear i temat. wpb är namnet på det tema vi arbetar med, det används här för att göra dessa strängar översättningsbara. You should replace it with your theme name.
Lägga till dynamiska sidebar som är redo för widgetar i WordPress Theme-filer
Hittills har vi bara registrerat Dynamic Sidebars. Användare kan drag and drop widgets till dessa sidebar från vyn Appearance ” Widgets. Dessa sidebars kommer dock inte att visas på din site förrän de anropas i en template som sidebar.php
eller någon annanstans där du vill visa dem. För att add to dessa widgetar areas, edit the template file where you want to display them and paste this code:
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?> <div id="secondary" class="widget-area" role="complementary"> <?php dynamic_sidebar( 'sidebar-1' ); ?> </div> <?php endif; ?>
I den här exempelkoden har vi använt sidebar id för att anropa det sidebar vi vill visa här. Ändra sidofältets ID:n för att visa ett annat sidofält. Du kan till exempel registrera tre sidebars för footer area och sedan anropa dem en efter en i temats footer.php
template.
Widgetar kan vara mycket kraftfulla. You can add widgets to your posts and page content, make your text widgets colorful, or extend the power of default WordPress widgets. Rätt placerade widgetklara sidebars allow användare att lägga till customize-element på sina websites med hjälp av ett enkelt drag and drop-gränssnitt.
Vi hoppas att den här artikeln hjälpte dig att lära dig hur du addar dynamiska widgetklara sidebar i WordPress. Vi rekommenderar att du studerar koden i teman som Genesis för att lära dig hur professionella använder dem i sina produkter. För frågor och återkoppling vänligen lämna en comment under.
nabil
hi i need two column in side bar ! how can i do that whit css
Jason
Where can i extend the functionality of my wordpress widgets
Milada
Hallo team again,
sorry to trouble you, but I would like to specify, that I would need one widget above the themes current Primary Sidebar widget on the right and the other widget on the left side from the article in the middle.
How should I proceed?
Thank you,
Milada
Milada
Hallo team again,
how do I add a field in the widget and which and where do I paste a field code to have there affiliate links?
Thank you,
Milada
Tanmay Bala
i want to learn how to use DB Plugin in wordpress and redux framework.
Marc
This tutorial was a great help.
Been trying to accomplish this for about a week now.
Nice and to-the-point.
Thank you!
Laura
I just wondered, before I actually paste the code, does this work on a theme that originally doesn’t come with a sidebar? I’m using Book Lite which only has footers.
WPBeginner Support
Yes, it does.
Administratör
Laura
Thanks so far – I tried to register the sidebar but whenever I hit ”save” I get the white screen of death – which I never had before. Any suggestions what could be wrong?
Lohith
Thanks Alot
Its really wonderful and simple method to explain….
Bakopu
Thank you very very much sir. Where are you sir , we want to take your blessings by touching your feet.
akhilesh
thanks sir these post have vry helpful for me
Marcos Nakamine
Worked perfectly
Thanks
gWorldz
I am having trouble getting the widget to display o.O
The widget is registering and displaying in the back-end properly with no issues, however, it will only show my widget content if I include content in the previous widget.
I’m using a 2011 child theme and registering an additional footer area.
functions.php snippet:
https://gist.github.com/gWorldz/76273b5901e1b5c4759b#file-functions-php
sidebar-footer.php
https://gist.github.com/gWorldz/b6177ccc9734718dd636#file-sidebar-footer-php
style.css
https://gist.github.com/gWorldz/0f24a9350da164ee2f29#file-style-css
Do you have any idea what I’m doing wrong or what is causing this issue?
Tiger
Did you ever get any help on this? I am having the same issue.
MJ
Just wanted to say thanks for this. I Googled to find out how to register a sidebar in WordPress, and hit a few other sites before this one, and yours had the best, clearest, most understandable directions. Well done.
arvind
your post is helpful to me,
thanks !!!!!!
Dharmendra Prajapati
Finally i got my dynamic widget ready sidebar after 23 days… I was googled all the sites.. and finally i redirected @ your site.
Thanks
Kevin
In the second snippet, where you are inserting the widget code into the template, where you have
div id=”secondary”
Should that id change with each widget area.
Ex:
First, Secondary, Third, fourth, etc
WPBeginner Support
Kevin yes you should, and it should be named after the widget area or the sidebar it is representing.
Administratör
SS
Works for registering the sidebars but when I go to the Appearance » Widgets screen I see all my widgets but nothing to drag them into.
WPBeginner Support
SS, when you have successfully registered a dynamic widget ready sidebar it will appear on Widgets screen. This means that registering sidebars probably didn’t work.
Administratör
Steve Smart
Hi –
This is a very helpful post. Thankyou!
I’ve been successful using one dynamic widget area, now I’m trying to implement more than one. Everything seems to work as expected, except an odd problem in the dashboard. If I drag a text widget to my second dynamic widget area I can add content as you would expect, and save it, and it produces output on my pages as expected, however if I now revisit the dashboard widgets page, the second widget area does not appear to contain any widgets.
I am using a twentytwelve-child theme, and Wordpress 3.7.1.
Any thoughts?
Jackson
Hearty thanks….You have done such a wonderful work…thanks thanks thanks…
JP
thanks, found this useful. right to the point.
Praveen Prasad
Can you tell me where is the Call Function Started ?
Ed Du
You can also use Headway with a additional block and it will take care of all of this. Or pagelines for that matter.