Tekniska bloggspel, Android-app-apk, tips och tricks

Hur man visar valfritt antal inlägg i en WordPress-loop

Obs: I nästa ämne du kommer att läsa kommer du att lära dig om: Hur man visar valfritt antal inlägg i en WordPress-loop

WordPress använder en loop för att visa alla dina inlägg. Med hjälp av Loops bearbetar WordPress varje inlägg för att visas på den aktuella sidan och formaterar dem på ett sådant sätt att de matchar kriterierna som anges i Loop-taggen. Normalt ställs antalet inlägg som ska visas i Inställningar-området på WordPress Admin Panel på läsfliken. Men i den här artikeln kommer vi att visa dig hur du åsidosätter det numret med Super Loop som låter dig visa valfritt antal inlägg i just den WordPress-slingan. Detta gör att du kan anpassa visningen av dina sidor, inklusive författarprofiler, sidofält och mer.

Öppna mallfilen där du vill lägga inlägget och lägg sedan till denna loop:

// if everything is in place and ready, let's start the loop
 


// to display 'n' number of posts, we need to execute the loop 'n' number of times
// so we define a numerical variable called '$count' and set its value to zero
// with each iteration of the loop, the value of '$count' will increase by one
// after the value of '$count' reaches the specified number, the loop will stop
// *USER: change the 'n' to the number of posts that you would like to display



// for CSS styling and layout purposes, we wrap the post content in a div
// we then display the entire post content via the 'the_content()' function
// *USER: change to '' to display post excerpts instead



// here, we continue with the limiting of the number of displayed posts
// each iteration of the loop increases the value of '$count' by one
// the final two lines complete the loop and close the if statement




Och du är klar. Den här koden kommer att vara praktisk för dig, särskilt när du designar författarmallen eftersom du vill kontrollera antalet inlägg som visas i varje slinga.

Källa: Super Loop för WordPress