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 wyświetlić ostatnie wpisy jako listę rozwijaną w WordPress?

Prawdopodobnie znasz widżet kategorii w WordPressie. Niedawno jeden z naszych czytelników zapytał nas, czy możliwe jest wyświetlanie ostatnich wpisów również w formie rozwijanej. W tym artykule pokażemy, jak wyświetlić ostatnie wpisy jako listę rozwijaną w WordPress.

Add recent posts as drop down menu

Dlaczego i kto potrzebuje ostatnich wpisów w rozwijanym menu?

WordPress posiada wbudowany widżet ostatnich wpisów, który można dodać do dowolnego panelu bocznego lub obszaru gotowego na widżet.

Ten widżet po prostu wyświetla listę ostatnich wpisów i możesz wybrać liczbę wpisów, które chcesz wyświetlić. Jeśli jednak chcesz wyświetlić więcej niż 5-10 wpisów, lista zajmie dużo miejsca na twoim panelu bocznym.

Niektórzy użytkownicy WordPressa mogą potrzebować kompaktowego sposobu wyświetlania najnowszych wpisów. W takim przypadku korzystanie z rozwijanych lub zwijanych list może pomóc zaoszczędzić miejsce.

Przyjrzyjmy się kilku różnym sposobom wyświetlania ostatnich wpisów jako rozwijanego menu w WordPress.

Wyświetlanie ostatnich wpisów WordPress w zwykłym rozwijanym menu (kod ręczny)

Ta metoda wykorzystuje wbudowaną funkcję wp_get_recent_posts. Wszystko, co musisz zrobić, to skopiować i wkleić poniższy kod do pliku functions. php twojego motywu lub wtyczki witryny.

function wpb_recentposts_dropdown() { 
$string .= '<select id="rpdropdown">
			<option  value="" selected>Select a Post</option>';

$args = array( 'numberposts' => '5', 'post_status' => 'publish' );

$recent_posts = wp_get_recent_posts($args);
	foreach( $recent_posts as $recent ){
		$string .= '<option value="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</option> ';
	}

$string .= '</select>
			<script type="text/javascript"> var urlmenu = document.getElementById( "rpdropdown" ); urlmenu.onchange = function() {
      		window.open( this.options[ this.selectedIndex ].value, "_self" );
 			};
			</script>';

return $string;
} 
add_shortcode('rp_dropdown', 'wpb_recentposts_dropdown');
add_filter('widget_text','do_shortcode');

.

Teraz możesz użyć krótkiego kodu [rp_dropdown] w twoim wpisie WordPress, stronach i widżetach tekstowych. Będzie to wyglądać następująco:

Recent posts in a drop down menu on a WordPress site

Dodawanie zwijanych wpisów za pomocą wtyczki

Powyższa metoda po prostu wyświetla twoje ostatnie wpisy w formie rozwijanej. Innym sposobem na zaoszczędzenie miejsca jest dodanie zwijanej listy ostatnich wpisów, która rozwija się po kliknięciu.

Pierwszą rzeczą, którą musisz zrobić, to zainstalować i włączyć wtyczkę Collapse-O-Matic. Działa ona od razu po wyjęciu z pudełka i nie ma żadnych ustawień do skonfigurowania.

Wtyczka pozwala po prostu wyświetlać wszystko w zwijanym menu za pomocą krótkiego kodu.

Zanim użyjemy tej wtyczki, potrzebujemy sposobu na łatwe wyświetlanie ostatnich wpisów w dowolnym miejscu. Wystarczy dodać ten kod do pliku functions. php twojego motywu lub wtyczki specyficznej dla witryny.

function wpb_recentposts() { 

$string .= '<ul>';
$args = array( 'numberposts' => '5', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts($args);
	foreach( $recent_posts as $recent ){
		$string .= '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a></li> ';
	}
$string .= '</ul>';
return $string;
} 
add_shortcode('recentposts', 'wpb_recentposts');
add_filter('widget_text','do_shortcode');

.

Ten kod pozwala po prostu wyświetlić listę dozwolonych ostatnich wpisów za pomocą krótkiego kodu [ SKX2].

Teraz dodamy nasz krótki kod w krótkim kodzie Collapse-O-Matic, aby utworzyć zwijaną listę ostatnich wpisów.

Wystarczy dodać krótki kod w następujący sposób:

[expand title="Recent Posts"][recentposts][/expand].

Możesz dodać ten krótki kod w widżecie tekstowym, wpisach lub stronach na twojej witrynie WordPress. Tak to wyglądało na naszej witrynie testowej.

Collapsable list of recent posts

To wszystko, mamy nadzieję, że ten artykuł pomógł ci wyświetlić ostatnie wpisy jako listę rozwijaną w WordPress. Możesz również zapoznać się z tymi 6 wskazówkami, aby stworzyć zabójczy kalendarz redakcyjny w WordPress.

Jeśli podobał Ci się ten artykuł, zasubskrybuj nasz kanał YouTube, aby zobaczyć poradniki dotyczące filmów WordPress. Możesz nas również znaleźć na Twitterze i Facebooku.

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

17 komentarzyZostaw odpowiedź

  1. Syed Balkhi

    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. Yvonne Manders

    I made a mistake and solved it.

    • WPBeginner Support

      Glad you were able to solve the issue :)

      Administrator

  3. Yvonne Manders

    Hi,

    I installed the collapse-o-mattic and pasted the code in the functions.php file.
    After that I pasted the shortcode [expand title=”Recent Posts”][recentposts][/expand] in a textwidget, but nothing happened.
    Am I missing something?

    Best regards, Yvonne

    • WPBeginner Support

      You would want to place the shortcode in the text editor rather than the visual editor to ensure you don’t have styling blocking the shortcode from working.

      Administrator

  4. Erika

    Line 3 has an error. The closing option tag is missing its closing „/”. This is generating a blank option in the menu.

    • WPBeginner Support

      Thank you for letting us know, the code should be fixed now :)

      Administrator

  5. Amjad

    Hi there. I have used above code to show all my posts in a drop down but its not sorted. I have two questions here:
    1. How can i sort posts by title?
    2. How can i show posts of a specific category in dropdown instead of showing all posts?

  6. Rolando

    Nice article! How can you make it by a specific category and not just all your categories? Also, can it be done alphabetical?

  7. Farai Mugaviri

    Thank you so much for the great help there. I wouldalso want to display categories in a dop-down list, if you can help with that…. But now what if I update my WordPress, is it even possible? I saw somewhere they talked about challengess when updating the wordpress and risking losing data because of hardcoding the PHP functions

  8. Robert

    Great tool, thanks for that!
    Is it possible to sort the post output in the list in alphabetical order?
    Thanks in advance for your reply!

    Regards,
    Robert

  9. Andre

    Its possible to show the posts dropdown with a button to submit?

    'function wpb_recentposts_dropdown() {
    $string .= ’
    Select your School’;

    $args = array( 'numberposts’ => '5′, 'post_status’ => 'publish’ );

    $recent_posts = wp_get_recent_posts($args);
    foreach( $recent_posts as $recent ){
    $string .= ” . $recent[„post_title”].’ ’;
    }

    $string .= ’
    FIND SCHOOL NOW
    var urlmenu = document.getElementById( „submitschool” ); urlmenu.onclick = function() {
    window.open( this.options[ this.selectedIndex ].value, „_self” );
    };
    ’;

    return $string;
    }
    add_shortcode(’rp_dropdown’, 'wpb_recentposts_dropdown’);
    add_filter(’widget_text’,’do_shortcode’);’

  10. Emily Johns

    Very neat Stuff!! Great Read and easy to understand.

  11. NG SHAIKH

    It is an excellent article. Beginners like me can understand power of WordPress and its plug-ins by such articles.

    I would be enlightened if some articles are written to display a message on specific page and not on all posts and pages.

    It will also help beginners if a few articles are written to display a form for user entry which can be saved in the database

  12. Julie S

    I really like the drop-down menu of recent posts. How can I control the width of this drop down menu? It defaults too long for my sidebar.

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