”The loop” brukar också anropas ”The WordPress loop”, eller helt enkelt bara ”loop”. Det är den viktigaste delen av PHP-koden som visar posterna på en WordPress website.
WordPress Loop är utan tvekan en av de viktigaste aspekterna av WordPress-koden och är på ett eller annat sätt core i de flesta sökningar.

Infografik: Förstå WordPress-loopen
Loopen används i WordPress themes för att visa en lista med posts på en page.
Temautvecklare kan formatera utdata genom att använda template-taggar för att customize hur varje post inuti loopen visas. Flera template-taggar fungerar bara inuti WordPress-loopen och används för att formatera, ordna och publicera postdata.
Vi har skapat följande infografik för att dela upp WordPress Loop för nybörjare.

Exempel på kod: En enkel WordPress-loop
Du kanske gillar att se ett exempel.
Här är lite PHP-kod som används för att bilda en enkel WordPress-slinga:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php // checks if there are any posts that match the query if (have_posts()) : // If there are posts matching the query then start the loop while ( have_posts() ) : the_post(); // the code between the while loop will be repeated for each post ?> <h2 id= "post-<?php the_ID(); ?>" ><a href= "<?php the_permalink() ?>" rel= "bookmark" title= "Permanent Link to <?php the_title(); ?>" ><?php the_title(); ?></a></h2> <p class = "date-author" >Posted: <?php the_date(); ?> by <?php the_author(); ?></p> <?php the_content(); ?> <p class = "postmetadata" >Filed in: <?php the_category(); ?> | Tagged: <?php the_tags(); ?> | <a href= "<?php comments_link(); ?>" title= "Leave a comment" >Comments</a></p> <?php // Stop the loop when all posts are displayed endwhile ; // If no posts were found else : ?> <p>Sorry no posts matched your criteria.</p> <?php endif ; ?> |
Vi hoppas att den här artikeln hjälpte dig att lära dig mer om WordPress-loopen. Du kanske också vill se vår lista med ytterligare Läsa under för relaterade artiklar om användbara WordPress-tips, tricks och idéer.
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.