Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
Puchar WPB
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Jak dodać panel boczny z dynamicznym widżetem w WordPress?

Uwaga redakcyjna: Otrzymujemy prowizję z linków partnerskich na WPBeginner. Prowizje nie mają wpływu na opinie i oceny naszych redaktorów. Dowiedz się więcej o Proces redakcyjny.

Widżety są tak integralną częścią motywów WordPress, że trudno wyobrazić sobie motyw WordPress bez widżetów. Widżety to wykonywalne skrypty, które można po prostu przeciągnąć i upuścić na panelu bocznym lub w dowolnym innym obszarze gotowym na widżet w twoim motywie. Wielu naszych klientów używa widżetów do dodawania własnych elementów do panelu bocznego. Ten artykuł jest jednak przeznaczony dla tych ciekawskich użytkowników, którzy chcą dowiedzieć się, jak dodać dynamiczne panele boczne lub obszary gotowe na widżety w motywach WordPress.

Rejestrowanie paneli bocznych lub obszarów gotowych na widżety w WordPressie

Pierwszą rzeczą, którą musisz zrobić, jest zarejestrowanie twojego panelu bocznego lub obszaru gotowego na widżet dla twojego motywu. Możesz zarejestrować wiele paneli bocznych i obszarów gotowych na widżety. Skopiuj i wklej ten kod do pliku functions.php twojego motywu.

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' );

.

W tym kodzie zarejestrowaliśmy dwa panele boczne. Nadaliśmy im nazwy i opisy, aby zidentyfikować je na ekranie widżetów. Parametr description może być użyty do poinformowania użytkowników, gdzie ten panel boczny pojawia się w motywie. Wpb to nazwa motywu, nad którym pracujemy, jest ona tutaj używana do przetłumaczenia tych ciągów znaków. Powinieneś zastąpić go nazwą twojego motywu.

Newly created sidebars appearing on Widgets screen

Dodawanie dynamicznych paneli bocznych gotowych na widżety w plikach motywu WordPress

Do tej pory zarejestrowaliśmy tylko dynamiczne panele boczne. Użytkownicy mogą przeciągać i upuszczać widżety do tych paneli bocznych z ekranu Wygląd ” Widżety. Jednak te panele boczne nie pojawią się na twojej witrynie, dopóki nie zostaną wywołane w szablonie, takim jak sidebar.php lub w innym miejscu, w którym chcesz je wyświetlić. Aby dodać te obszary widżetów, edytuj plik szablonu, w którym chcesz je wyświetlić i wklej ten kod:

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
	<div id="secondary" class="widget-area" role="complementary">
	<?php dynamic_sidebar( 'sidebar-1' ); ?>
	</div>
<?php endif; ?>

.

W tym przykładowym kodzie użyliśmy identyfikatora sidebar do wywołania panelu bocznego, który chcemy tutaj wyświetlić. Zmień identyfikator sidebar id, aby wyświetlić inny panel boczny. Możesz na przykład zarejestrować trzy panele boczne dla obszaru stopki, a następnie wywołać je jeden po drugim w szablonie footer.php twojego motywu.

Widżety mogą być bardzo potężne. Możesz dodać widżety do swoich wpisów i treści strony, sprawić, że twoje widżety tekstowe będą kolorowe lub rozszerzyć moc domyślnych widżetów WordPress. Odpowiednio umieszczone panele boczne gotowe na widżety pozwalają klientom dodawać własne elementy do swoich witryn internetowych za pomocą prostego interfejsu przeciągnij i upuść.

Mamy nadzieję, że ten artykuł pomógł ci nauczyć się dodawać dynamiczne panele boczne gotowe na widżety w WordPress. Zalecamy zapoznanie się z kodem frameworków motywów, takich jak Genesis, aby dowiedzieć się, jak profesjonaliści wykorzystują je w swoich produktach. W przypadku pytań i uwag prosimy o pozostawienie komentarza poniżej.

Ujawnienie: Nasze treści są wspierane przez czytelników. Oznacza to, że jeśli klikniesz na niektóre z naszych linków, możemy otrzymać prowizję. Zobacz jak WPBeginner jest finansowany, dlaczego to ma znaczenie i jak możesz nas wspierać. Oto nasz proces redakcyjny.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Najlepszy zestaw narzędzi WordPress

Uzyskaj BEZPŁATNY dostęp do naszego zestawu narzędzi - zbiór produktów i zasobów związanych z WordPressem, które każdy profesjonalista powinien mieć!

Reader Interactions

28 komentarzyZostaw odpowiedź

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Milada says

    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

  3. Milada says

    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

  4. Marc says

    This tutorial was a great help.
    Been trying to accomplish this for about a week now.
    Nice and to-the-point.

    Thank you!

  5. Laura says

    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.

  6. gWorldz says

    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?

  7. MJ says

    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.

  8. Dharmendra Prajapati says

    Finally i got my dynamic widget ready sidebar after 23 days… I was googled all the sites.. and finally i redirected @ your site.

    Thanks

  9. Kevin says

    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

  10. SS says

    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.

  11. Steve Smart says

    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?

  12. Ed Du says

    You can also use Headway with a additional block and it will take care of all of this. Or pagelines for that matter.

Zostaw odpowiedź

Dziękujemy za pozostawienie komentarza. Pamiętaj, że wszystkie komentarze są moderowane zgodnie z naszymi polityka komentarzy, a Twój adres e-mail NIE zostanie opublikowany. NIE używaj słów kluczowych w polu nazwy. Przeprowadźmy osobistą i konstruktywną rozmowę.