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ć widżet WordPress do nagłówka twojej witryny internetowej (2 sposoby)

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.

Chcesz dodać widżet WordPress do obszaru nagłówka twojej witryny internetowej?

Widżety umożliwiają łatwe dodawanie bloków treści do określonych sekcji twojego motywu, ale nie każdy motyw zawiera obszar widżetów nagłówka.

W tym artykule pokażemy, jak łatwo dodać widżet WordPress do nagłówka twojej witryny internetowej.

How to add a WordPress widget to your website header (2 ways)

Dlaczego warto dodać widżet nagłówka do twojej witryny WordPress?

Nagłówek Twojej witryny internetowej jest jedną z pierwszych rzeczy, które zobaczą odwiedzający, gdy przejdą na Twoją witrynę WordPress. Dodając widżet WordPress do twojego nagłówka, możesz zoptymalizować ten obszar, aby przyciągnąć uwagę czytelników.

Większość nagłówków witryn internetowych zawiera własne logo i menu nawigacyjne, które pomaga odwiedzającym poruszać się po twojej witrynie.

Możesz również dodać widżet nagłówka powyżej lub poniżej tego obszaru, aby pełnić funkcję przydatnych treści, banerów reklamowych, ofert ograniczonych czasowo, formularzy jednowierszowych i innych.

W WPBeginner mamy wezwanie do działania w nagłówku bezpośrednio pod menu nawigacyjnym.

Header CTA example

Większość motywów WordPress posiada obszary gotowe na widżety w panelu bocznym i stopce witryny, ale nie każdy motyw dodaje obszary gotowe na widżety w nagłówku.

Uwaga redaktora: Jeśli twój motyw nie ma obszaru nagłówka gotowego na widżet, możesz teraz tworzyć całkowicie własne konfiguratory WordPress od podstaw (bez żadnego kodowania).

Najpierw jednak przyjrzyjmy się krok po kroku, jak dodać widżet WordPress do nagłówka twojej witryny internetowej w istniejącym motywie. Skorzystaj z poniższych odnośników, aby przejść bezpośrednio do wybranej metody:

Metoda 1: Dodanie widżetu WordPress do nagłówka twojej witryny internetowej w ustawieniach motywu WordPress

Wiele z najlepszych motywów WordPress zawiera obszar widżetów nagłówka, który możesz dostosować do własnych upodobań.

Po pierwsze, będziesz chciał sprawdzić, czy twój obecny motyw WordPress obsługuje obszar widżetów WordPress w nagłówku.

Możesz to znaleźć, przechodząc do konfiguratora motywu WordPress lub obszaru administracyjnego widżetów w twoim panelu administracyjnym WordPress. Aby to zrobić, przejdź do Wygląd ” Dostosuj i sprawdź, czy istnieje opcja edycji nagłówka.

Uwaga: Jeśli używasz motywu blokowego, nie zobaczysz tej opcji na panelu bocznym administratora. Zamiast tego powinieneś zapoznać się z naszym przewodnikiem na temat tego, jak dostosować własny nagłówek za pomocą WordPress Full Site Editor.

W tym przykładzie darmowy motyw Astra ma opcję o nazwie „Header Builder”. Pokażemy ci, jak korzystać z tej funkcji w Astrze, ale pamiętaj, że będzie to wyglądać inaczej w zależności od używanego motywu.

Astra header builder

Kliknięcie tego przycisku spowoduje przejście do ekranu umożliwiającego edycję twojego nagłówka i dodawanie widżetów.

W dolnej części ekranu można całkowicie dostosować nagłówek wraz z obszarami nad i pod nagłówkiem. Wystarczy najechać kursorem na jeden z pustych obszarów i kliknąć ikonkę „Plus”.

Click plus icon

Spowoduje to wyświetlenie menu podręcznego, w którym można wybrać „Widżet 1”.

Istnieją dodatkowe opcje do wyboru, ale musisz wybrać jedną z opcji „Widżet”, aby nagłówek był gotowy na widżet.

Select widget 1 option

Aby dodać obszar widżetów do twojego nagłówka, kliknij pole „Widżet 1”, które znajduje się w sekcji konfiguratora nagłówka.

Spowoduje to wyświetlenie opcji dodania widżetu.

Click widget 1 box

Następnie kliknij ikonkę dodawania bloku „Plus” w menu po lewej stronie.

Spowoduje to wyświetlenie wyskakującego okienka, w którym można wybrać widżet do dodania do twojego nagłówka.

Click plus icon and select widget

Możesz kontynuować dostosowywanie własnego nagłówka i dodawać dowolną liczbę widżetów.

Gdy skończysz, kliknij przycisk „Opublikuj”, aby zapisać twoje zmiany.

Publish header widget changes

Teraz możesz zobaczyć swój obszar nagłówka z widżetem lub widżetami, które dodałeś.

Oto zrzut ekranu pokazujący, jak to wygląda na naszej witrynie demonstracyjnej.

Header widget example

Nie używasz Astry?

Innym sposobem sprawdzenia, czy twój motyw ma już widżet nagłówka WordPress, jest przejście do Wygląd ” Widżety w panelu administracyjnym WordPress.

Następnie sprawdź, czy istnieje sekcja widżetu oznaczona jako „Nagłówek” lub coś podobnego.

Go to widgets section for header widget

Jeśli tak, wystarczy kliknąć ikonkę bloku dodawania „Plus”, aby wyświetlić menu widżetów.

Następnie można dodać dowolny widżet, klikając go.

Header widget section

Upewnij się, że kliknąłeś przycisk „Aktualizuj”, aby zapisać twoje zmiany w obszarze widżetu nagłówka.

Metoda 2: Dodanie widżetu WordPress do nagłówka twojej witryny internetowej poprzez dodanie kodu do WordPressa

Jeśli twój motyw WordPress nie ma obszaru widżetów WordPress w nagłówku, musisz dodać go ręcznie, dodając kod do WordPress.

Jeśli nie robiłeś tego wcześniej, zapoznaj się z naszym przewodnikiem na temat kopiowania i wklejania kodu w WordPress.

Następnie możesz dodać następujący fragment kodu do twojego pliku functions.php lub za pomocą wtyczki fragmentów kodu, takiej jak WPCode (zalecane):

function wpb_widgets_init() {

	register_sidebar( array(
		'name'          => 'Custom Header Widget Area',
		'id'            => 'custom-header-widget',
		'before_widget' => '<div class="chw-widget">',
		'after_widget'  => '</div>',
		'before_title'  => '<h2 class="chw-title">',
		'after_title'   => '</h2>',
	) );

}
add_action( 'widgets_init', 'wpb_widgets_init' );

Aby uzyskać więcej informacji, zapoznaj się z naszym przewodnikiem na temat dodawania własnego kodu w WordPressie bez niszczenia twojej witryny.

Przeprowadzi Cię przez metodę WPCode.

Adding the Code Snippet to WPCode

Ten kod rejestruje nowy panel boczny lub obszar gotowy na widżet dla twojego motywu.

Jeśli przejdziesz do Wygląd ” Widżety, zobaczysz nowy obszar widżetów oznaczony jako „Obszar widżetów własnego nagłówka”.

Custom header widget area

Teraz możesz dodać swój widżet do tego nowego obszaru. Więcej szczegółów można znaleźć w naszym przewodniku na temat dodawania i korzystania z widżetów w WordPress.

Jednak twój widżet nagłówka nie będzie jeszcze wyświetlany na żywo na witrynie internetowej. Pokażemy ci, jak to zrobić w następnej kolejności.

Wyświetlanie własnego widżetu nagłówka w WordPressie

Teraz, gdy utworzyłeś obszar widżetu nagłówka, musisz powiedzieć WordPressowi, gdzie ma go wyświetlać na twojej witrynie internetowej.

Aby to zrobić, musisz edytować plik header.php twojego motywu. Następnie należy dodać następujący kod w miejscu, w którym ma być wyświetlany widżet:

<?php

if ( is_active_sidebar( 'custom-header-widget' ) ) : ?>
    <div id="header-widget-area" class="chw-widget-area widget-area" role="complementary">
	<?php dynamic_sidebar( 'custom-header-widget' ); ?>
    </div>

<?php endif; ?>

Ten kod doda utworzony wcześniej własny obszar widżetów do obszaru nagłówka twojej witryny internetowej.

Teraz możesz przejść na swojego bloga WordPress, aby zobaczyć swój obszar widżetu nagłówka na żywo.

Header widget live

Styl twojego widżetu w nagłówku WordPress za pomocą CSS

W zależności od twojego motywu może być również konieczne dodanie CSS do WordPressa, aby kontrolować sposób wyświetlania obszaru widżetu nagłówka i każdego widżetu w nim. Można to zrobić na kilka sposobów:

Aby dowiedzieć się więcej, zapoznaj się z naszym przewodnikiem na temat łatwego dodawania własnych CSS do twojej witryny WordPress.

W tym artykule przyjrzymy się, jak dodać własny CSS za pomocą wtyczki WPCode i konfiguratora motywu WordPress.

W metodzie 2 powyżej pokazaliśmy, jak użyć WPCode, aby dodać fragment kodu do twojego pliku functions.php. Możesz również dodać tę wtyczkę, aby dodać własny CSS.

Podczas tworzenia nowego fragmentu kodu w WPCode, upewnij się, że wybrałeś rodzaj kodu „CSS Snippet” z rozwijanego menu po prawej stronie i że opcja „Auto Insert” jest zaznaczona.

CSS Code Snippet in WPCode

W sekcji podglądu kodu musisz dodać CSS do stylizacji twojego widżetu nagłówka.

Oto przykładowy kod CSS, który pomoże ci zacząć:

div#header-widget-area {
    width: 100%;
    background-color: #f7f7f7;
border-bottom:1px solid #eeeeee;
    text-align: center;
		padding: 20px;
}
h2.chw-title {
    margin-top: 0px;
    text-align: left;
    text-transform: uppercase;
    font-size: small;
    background-color: #feffce;
    width: 130px;
    padding: 5px;
    }

Następnie należy upewnić się, że włączone jest ustawienie „Aktywny”, a następnie kliknąć przycisk „Zapisz fragment kodu” w prawym górnym rogu ekranu.

Aby zamiast tego użyć konfiguratora motywu, należy przejść do Wygląd ” Dostosuj w panelu administracyjnym WordPress. Spowoduje to wyświetlenie panelu konfiguratora motywu WordPress. Należy kliknąć kartę „Dodatkowe CSS”.

WordPress customizer additional CSS

Pozwala to na dodanie dodatkowego CSS bezpośrednio do twojego motywu i zobaczenie zmian w czasie rzeczywistym.

Po prostu dodaj kod CSS, który pokazaliśmy powyżej, do pola „Dodatkowy CSS”.

Add CSS code and publish

Po zakończeniu dodawania twojego CSS, upewnij się, że kliknąłeś przycisk „Opublikuj”, aby zapisać zmiany.

Oto jak wygląda własny widżet nagłówka ze zmianami CSS na żywo.

Header widget example after CSS

Mamy nadzieję, że ten artykuł pomógł ci dowiedzieć się, jak dodać widżet WordPress do nagłówka twojej witryny internetowej. Zachęcamy również do zapoznania się z naszym przewodnikiem na temat wyboru najlepszego hostingu WordPress oraz wyborem najlepszego oprogramowania do czatu na żywo dla małych firm.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

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

83 komentarzeZostaw 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. Rob Hartman says

    This tutorial was PERFECT and I’ve successfully created the header widget, super light and no impact on page speed. I had hoped that I could use the same approach to add a widget area to my footer, and replace all Header related reference in both the Functions and footer.php – but in this instance the widget area was not registered.

    Does this approach ONLY apply to the header area?

    Regardless, thanks for a super concise tutorial.

    • WPBeginner Support says

      The code should work even in a footer but your theme may not display the footer on every page as a common reason.

      Administrator

  3. Sjoerd says

    Hi! Thanks for the tutorial. What if I want to show the widget area above the primary menu instead of right below it?

    • WPBeginner Support says

      That would depend on your specific theme, we would first recommend reaching out to your specific theme’s support to see if they have a built-in method for what you’re wanting to do or have recommended settings.

      Administrator

  4. Muhammad Kashif says

    Hello, I added the custom header widget successfully, but after updating the theme my custom header widget is not working ? Please tell me the solution! Looking forward to your response. thanks

  5. Jim Rolt says

    Excellent code snippets which I was able to adapt and style to do the jobs I wanted. Many thanks for this

  6. Vildan says

    Hi WP Beginner,

    How do I get the widget area in the header on the right side of the menu? I want to add the ADD TO CART icon in there.

    • WPBeginner Support says

      For placement, it would depend on the specific theme you are using, if you check with your theme’s support they should be able to let you know what part of your header.php file to add the code to or if they have a built-in method to do so.

      Administrator

  7. Bathri Vijay says

    Thank You WPBeginner Editorial Team giving a perfect blog to the beginners and middle class coders. It is a big platform to learn and to execute WordPress. You were the big reason for the success of many bloggers and business owners.

  8. zambol says

    Thanks for the tutorial. it works but the trouble I have is that it’s not responsible for mobile devices. is it possible to make the sidebar responsible?

    • WPBeginner Support says

      I believe you mean responsive in which case, we sadly do not have a recommended method for making this widget responsive at the moment.

      Administrator

    • WPBeginner Support says

      Depending on your specific theme, if there is content before the header.php content, you would want to reach out to the theme’s support

      Administrator

  9. dav says

    hello,
    I would like to make an header like the one in the first picture on the top of this page ( the list 25 website).
    How can I make It?
    Please help me!

  10. GoosePT says

    Hello!

    Perfect tutorial.

    Im using it to create a Topbar, but im facing a problem the widgets (in my case 2) are one below the other.

    How can I make them to be displayed side by side?

  11. J M Das says

    This tutorial is very useful.
    How to remove the white space above this widget area in Posts? Thanks for any help

  12. Mikhail Koval says

    What would I have to put in the header.php to ONLY show on the home page but not show on other pages?

      • Anna says

        Hi, this is really what I’m looking for and it would be great if I could get it to work! Creating the widget area works fine, displaying it does unfortunately not.. I would also like to display it only on the start page.
        I´m not sure
        1) where in the header file to put the display code
        2) where to put this „only-on-startpage”-code and what code goes on line 2
        I understand if my questions are too basic to bother with, but I would be really grateful if someone has time and patience for it anyway..:-) Thanks in advance!!

        • WPBeginner Support says

          Hi Anna,

          These are perfectly reasonable questions.

          1. You need to put the display code based on where you want to display the header widget. For example, your theme’s header.php file may have a section to display site title, logo, description, and navigation menus. They will probably be wrapped around div tags. You will put your header widget code before or after these areas. If you are unsure, you can place the code at the end of the header.php file.

          2. If you only want to display the widget area on your website’s front page then you can use conditional tags like this:

          if ( is_front_page() ) {
          // your widget display code goes here 
          
          } 
          
        • Anna says

          OK, I cheated and put the widget-code in the front-page.php instead, it worked! Thanks :-)!

    • Anna says

      Hi again, it became visible on start page with css positioning!

      Now it’s only the question of how to put together the only-on-start-page-code with the visibility code…

      Thanks again for kindness and patience..)

  13. Paul Johnson says

    Great tutorial thanks. I am trying to implement the JetPack Cookie Consent Widget and despite following your instructions the Cookie Consent bar still appears at the bottom of the page.

    Is it possible the widget is overriding the code?

  14. Keu says

    Does this still work for Twentyseventeen? The functions.php part works fine, but the header.php part doesn’t.

  15. thomas evans says

    Hello
    pls can someone here help me as how to make this responsive on any mobile device?

    i want to insert a google adsense code here for ads 728 x 90 banner sizes but any time i do so, it shows perfectly on desktops but on mobile devices it becomes weird.
    thank you

  16. Anubhav Bhatt says

    I have successfully created a header widget and It was really easy, thanks to you all. But I actually wanted it in the POSTS, not on the Homepage because I’m using Nikko Portfolio theme and it looks awful.
    Please Help.

  17. Jeffrey says

    I placed the code in my functions.php file and added my widget code in the header.php file and added my widget text and saved.
    When I move on to editing pages, upon clicking Update, I get a white screen and can’t see my page.
    I then have to go back to /wp-admin to see the dashboard. There are no console errors when I click update but just a white screen.

  18. Derek says

    This is great for that header advertizment thing, but what I want to do is add a pay pal donation button to my header. (Right side) cant believe there is no video tutorials out there on how to make a a widgetized header space to drag your pay pal widget into so that it appears on the right side of your header.. i can imagine there is alot of people who could benefit from an instructional on this.

  19. Stephen Crawford says

    This is a great tutorial, but I would like to create a widget area that can be placed above the header area would that also be possible?

  20. Joey says

    Just updated everything and it’s working great on my desktop. Now, if you could tell me how to get the widget to display on my mobile device that would be great! Did I mess something along the way?

  21. Mark says

    Hi, many thanks for writing this, it has helped me massively. However, with my website: how do I get the widget to appear at the far right hand side. When I use the option to show search field in the header, this is the place I want it to go?

    Thanks in advance! :)

  22. Linda Holiner says

    I am confused. I don’t understand how to get an image into the widget and let the user change the image. I don’t see where your image is coming from.

  23. Thomas says

    Would it be possible to make a tutorial about how to make a TopBar with sections (Social media icons, Notice, Search, Woocommerce cart).

    No plugin does really do that well

    Thanks

  24. Tim says

    If i want to add multiple custom widget areas do i need to add a new function to functions.php for each? or just add them in the area i want?

  25. fahad says

    I managed to use the widget in the navigation bar instead of the header area as I wanted to add a language toggle option at this section. I have a small problem where I can’t move the language two flags to the any horizontal location. I tried to add margin-right: 100px; to the advanced CSS section but it didn’t change. It might be something else centering all the navigation content but I want to make sure if it could be fixed with the widget.

    • Courtney says

      Hey how did you edit the code to put it in the nav bar? I’m currently trying to do the same thing without any luck.

  26. Leanore says

    When I attempt to add more than one custom widget area, I receive a 500 Error on my website. Is there a reason for this? Is it possible to create more than one custom widget area for a theme?

  27. nina says

    Hi! When i try to update the function.php file, there is this fatal error:
    Fatal error: Cannot redeclare wpb_widgets_init()
    I am not sure if i did a wrong edit. Could you help

    • WPBeginner Support says

      Hey Nina,

      This means that wpb_widgets_init function already exists in your functions.php file. You will have to replace it like wpb2_widgets_init to make it unique or remove the previous code that you added.

      Administrator

  28. Sam says

    Hello,

    Thanks so much for this, it is really helpful.

    One issue I am running into: My widget is just a menu of a couple of text links to pages. Here is my site:

    See how the text is all the way against the right side of the header area? I would like to use a right margin to line it up under „Contact” without turning the margin area white.

    Also, would love to make the text black. :)

    Many thanks if you can help with these tweaks!

    Sam

  29. Tommy says

    Your blog is most helpful. Thank you for helping us made modifications to Twenty Seventeen. People like you make using a new wordpress theme like Twenty-seventeen much easier.

    I am relatively new to word press and would like to try some additional modifications to Twenty seventeen.

    I would like to create a new widget for wp pages. I am trying to figure out how I can take your blog information on how to make a new widget for the header and apply it to a wp page in the twenty seventeen theme. I am getting stuck.

    Could you show us now to create a new widget for a page? That would be awesome.

    Thanks
    Tommy

  30. Bobby_qw says

    Thank you very much Mr. Admin.You are teahing to us 'how to make our own web site. l always follow your posting and l make my own web site. Thank you very much.

  31. Shane Cunningham says

    I used this and though it worked in adding a custom widget area to the top of my page, i could not get it to align with the other items in the header (company logo and nav menu) in Cherry. No matter where I put it in the header.php, it either appeared above or below the other items. I wanted to use it to create a phone number block between the logo and the nav menu. Though I could get it to center in correct spot horizontally, i could not get it to appear between them. To give you a better idea of what I need, imagine that on this very page’s header, you wanted to move the nav menu justified to the right and put your 800 number between it and the „wpbeginner” logo. How could I use this code (or a variation of it) to do that?

  32. Kristin says

    Thank you so much for this (and all) of your detailed tutorials! Could you please tell me how to implement a header widget, excluding the Homepage?

    • Massimo says

      In the code that you added to header.php change the first line:
      if ( is_active_sidebar( 'custom-header-widget' ) ) : ? >
      with this one:
      if ( is_active_sidebar( 'custom-header-widget' ) && ! is_front_page ) : ?

    • Massimo says

      Sorry, I missed something!
      The line that you have to add is:
      if ( is_active_sidebar( 'custom-header-widget' ) && ! is_front_page() ) : ?>

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ę.