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

Hur man utesluter senaste post från WordPress Post Loop

När vi hjälper användare att konfigurera sina WordPress-bloggar får vi ofta frågor om hur man anpassar layouten, särskilt hur man får det senaste inlägget att verkligen sticka ut.

Ibland vill du inte att det nyaste inlägget bara ska smälta in i resten av bloggflödet. Du kanske har en speciell design i åtanke eller vill att det ska visas på en helt annan plats.

Det är här som exkludering av det senaste inlägget från WordPress inläggsslinga kommer in i bilden. Detta ger dig flexibiliteten att visa det senaste innehållet hur du vill. I den här guiden visar vi dig två sätt att utesluta det senaste inlägget från WordPress inläggsloop.

How to exclude latest post from the WordPress post loop

Varför utesluta det senaste inlägget från WordPress Post Loop?

Som standard visar WordPress dina blogginlägg för besökarna i omvänd kronologisk order, så att de nyare inläggen visas först. Men du kanske vill ändra hur det senaste inlägget ser ut på din customize home page eller WordPress archive page.

Genom att utesluta det senaste inlägget från WordPress postloop kan du visa det innehållet på en annan location och till och med add to your own custom styling.

Med det sagt, låt oss se hur du kan dölja det senaste blogginlägget genom att utesluta det från WordPress inläggsslinga. Använd bara snabblänkarna under för att hoppa direkt till den metod du vill använda:

Det enklaste sättet att utesluta det senaste inlägget från postslingan är att lägga till kod på din WordPress website.

Vissa guider säger att du ska editera theme-filerna manuellt, men detta kan orsaka vanliga WordPress-fel och kan till och med helt förstöra din website.

Av den anledningen rekommenderar vi att du använder WPCode. Det är det bästa code snippet plugin som gör det enkelt att add to custom kod i WordPress utan att sätta din site i riskzonen.

Först måste du installera och aktivera det gratis WPCode-pluginet. För mer information, se vår Step-by-Step guide om hur du installerar ett WordPress plugin.

När plugin är aktiverat, gå till Code Snippets ” Add Snippet.

Adding custom code snippets to WordPress

Här ser du alla de färdiga snippets som du kan lägga till på din webbplats. Dessa inkluderar ett utdrag som låter dig helt inaktivera kommentarer, ladda upp filtyper som WordPress vanligtvis inte stöder, inaktivera bifogade sidor och mycket mer.

Du kan nu hovera musen över alternativet ”Add Your Custom Code (New Snippet)” och klicka på knappen ”Use snippet” när det visas.

Adding a custom code snippet to your WordPress website using WPCode

På nästa vy skriver du in en titel för code snippet. Detta är bara för din referens, så du kan använda vad du vill.

Sedan måste du öppna ”Code Type” dropdown och välja ”PHP Snippet”.

Excluding latest blog from the WordPress post loop

När det är gjort klistrar du bara in följande i kod editor:

<?php
function wpsites_exclude_latest_post( $query ) {
	if ( $query->is_home() && $query->is_main_query() ) {
		$query->set( 'offset', '1' );
	}
}

add_action( 'pre_get_posts', 'wpsites_exclude_latest_post', 1 );

Därefter rullar du ner på sidan till section ”Insertion”.

Om den inte redan är vald väljer du ”Auto Insert” och ”Run Everywhere”.

Automatically inserting code into a WordPress website

Slutligen rullar du till högst upp på vyn och clickar på slidern ”Inaktiverad” så att den visar ”Aktiv”.

Sedan klickar du bara på knappen ”Save Snippet” eller ”Update” för att göra code snippet live.

Excluding latest blog from the WordPress post loop using WPCode

Nu, om du besöker din WordPress website, kommer det senaste blogginlägget att vara dolt.

Metod 2: Editera WordPress Theme Files (inget plugin obligatoriskt)

Notes:: Denna tutorial fungerar endast med klassiska WordPress themes, eftersom block themes har en annan uppsättning theme-filer. Om du använder ett block theme föreslår vi att du använder den första metoden.

Vi rekommenderar också att du säkerhetskopierar din website först och/eller använder en staging-miljö för att förhindra error när du följer den här metoden.

Ett annat sätt att utesluta det senaste inlägget från WordPress postloop är genom att lägga till kod i dina WordPress theme-filer.

Detta ger ett liknande resultat som koden ovan, men du måste add to den direkt till WordPress-slingan där du vill att den ska visas.

Om du till exempel vill ändra slingan för post på hela din website måste du add to koden till filen index.php.

Tänk bara på att om du lägger till kod direkt i dina WordPress-filer kan det orsaka issues med sidonumrering på din WordPress-blogg.

Först måste du öppna filhanteraren hos din WordPress hosting provider eller ansluta din website till en FTP-klient.

För denna tutorial kommer vi att använda Bluehosts filhanterare eftersom processen är mycket enklare än att använda en FTP. Med detta sagt är stegen mer eller mindre desamma oavsett om du använder en FTP eller ett annat webbhotell.

Om du är användare av Bluehost loggar du in i adminpanelen på ditt webbhotell och navigerar till tabben ”Websites”. Klicka sedan på ”Settings” på den website du gillar att edit.

Bluehost site settings

Gå nu till sektionen Quick Links.

Klicka sedan på knappen ”File Manager”. Bluehost visar dig också din websites root directory om du behöver det.

Accessing a website's file manager in Bluehost

När du väl är inne i filhanteraren måste du lokalisera den fil du vill editera. Låt oss säga att du vill add to koden till index.php, då hittar du den i ditt nuvarande temas folder, som ligger i public_html/wp-content.

Väl där högerklickar du bara på filen index.php och klickar på ”Edit”.

Opening the index.php file in Bluehost file manager

Nu måste du kopiera följande kod och add to din WordPress loop:

query_posts('posts_per_page=6&offset=1');

Den här koden säger till slingan att bara visa 5 posts efter det senaste inlägget.

You will need to add the code directly above your WordPress loop so it will look similar to the code snippet below:

<?php
query_posts('posts_per_page=6&offset=1');
if ( have_posts() ) {

	// Load posts loop.
	while ( have_posts() ) {
		the_post();

		get_template_part( 'template-parts/content/content', get_theme_mod( 'display_excerpt_or_full_post', 'excerpt' ) );
	}

	// Previous/next page navigation.
	twenty_twenty_one_the_posts_navigation();

} else {

	// If no content, include the "No posts found" template.
	get_template_part( 'template-parts/content/content-none' );

}

Så här ser vår kod ut i Bluehosts filhanterare:

Adding code to exclude the latest post in Bluehost file manager

När du har addat koden klickar du bara på knappen ”Save Changes”.

Gå sedan vidare och preview din WordPress website.

Lär dig fler tips och tricks för att hantera dina WordPress-inlägg

Vill du optimera din WordPress-blogg ytterligare? Kolla in dessa guider:

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du utesluter det senaste inlägget från WordPress-loopen. Du kanske också vill se vår guide om hur du lägger till inlägg och sidor i bulk i WordPress och våra expertval av de bästa relaterade inläggs-pluginsen för WordPress.

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.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

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.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

42 kommentarerLämna ett svar

  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. BGH_ says

    Hey guys, thanks for this article! I want to implement this, but whenever I try I’ve got an strange problem: entries would look like exactly like the frontpage, showing a list of recent post rather than the post itself. My code is slightly different, since is a SMTheme. Any help will be much appreciated. PS: Sorry for posting twice, but the code has printed wrong. Here you go:

  3. space says

    Solution is work fine, Thanks. but pagination broke after applying this code, I have tried but not able to work with correct pagination. Any solution to pagination ? .

    • Rickie Anand says

      apply class to the li as per the number of page and a counter & give style to p1c1 as display:none;

  4. Sabi Maharjan says

    I have used two plugins. One is ”Recent facebook post” to show recent posts in wprdpress and ”Facebook publish page” to publish my wordpress posts in facebook . Now I want exclude the recent post of the facebook that was already posted from my wordpress posts .

    How can I do it ?

    • WPBeginner Support says

      For that your plugin posting content from your Facebook page should have a check to see if a post was already published from WordPress to Facebook. We do not recommend users to directly edit plugin files. However, if you are comfortable editing php files then you can fork the plugin as a new plugin and then add the code to accomplish this.

      Administratör

  5. Jim says

    Hmm, when i tried this code on a category page loop, it couldn’t keep the posts for only that category, instead it acted as if it was the front page loop and displayed all posts.

  6. PabloCovarrubias says

    hello, i just have one question, when i do this, pagination doesen0t work anymore, how can i fix this?

    thanks, other than that, it works perfectly!!

    cheers!

        • de hams says

          pagination do not work on index page most so we can make out own ajax pagination call and mostly query_posts() works for pagination rather then WP_Query()

  7. Wordpwess says

    Is there a way to offset a post from one cat?

    E.g I want to show all posts apart from the LATEST post of category x

  8. Khurram says

    ”query_posts(’posts_per_page=6&offset=1’);” this is not worked for me , while i am using the WP-PageNavi plugin for page navigation. :(

  9. Ken says

    Thanks for the info. Worked well :) Though, I’m encountering another issue for another site I’m currently doing. Is there a way to exclude from the loop only the latest post of a particular category?

    Thing is, I’ve a blog that publishes podcasts. The front page highlights the most recent podcast – posts are published under category: Podcast – via its own styling. At the same time, the front page also shows the last 5 most recent posts, of which may include posts under the category ”podcast”. I just don’t want to show in the loop the most recent post published under ”Podcast” simply because it’s already featured with it’s own styling. Hope you can help me out. thanks!

  10. Joffrey says

    Thanks for the tip!
    But I was wondering if there is also a way to exclude the post that is currently showed on screen.

    If yes, can you tell me how to do this?

    Thanks in advance!

      • Joffrey says

        One of my pages displays first of all the latest or current post the user is viewing.

        The current post is called by the loop.

        Below this post I want to display the excerpts of other previous posts without the current post displayed in this list.

        The previous post-excerpts are called by query_posts:

        query(’showposts=3&cat=5’);
        while($previousPosts->have_posts()) : $previousPosts->the_post();
        ?>

        As you can see I call 3 posts(excerpts) from category 5 which displays three posts from category News. This seems ok but within these 3 post-excerpts, the current post which is called by the loop is also in this list.
        Do you know a way to exclude the current post within this list?

        Thanks again!

        • Joffrey says

          Thanks again for the quick response!
          However, I did try the offset=1 in the query, only thing is that when you press an older post, you get to see the older post in the main loop, within the list excerpts the latest post is excluded, but the older post which is now the current post is still in the excerpts list.

          Any idea how to exclude not the latest but the current post?

          Thanks so far already :)

  11. Bilal Ahmed says

    Thank you so much for this piece as i was looking for a way to exclude 4 latest posts form my home page and i got answer by this articles and i have replaced (query_posts(’posts_per_page=6&offset=1’);) by query_posts(’posts_per_page=6&offset=3’); and its done :)

    • Editorial Staff says

      That is a multi-step process. First you would need to have a custom page template. Second you would need to create that custom page, your front page. Then you would run a query on that custom template showing only sticky posts.

      Administratör

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.