Die Schleife wird von WordPress verwendet, um jeden Ihrer Beiträge anzuzeigen. Mit The Loop verarbeitet WordPress jeden der auf der aktuellen Seite anzuzeigenden Posts und formatiert sie entsprechend den angegebenen Kriterien innerhalb der The Loop-Tags. Normalerweise wird die Anzahl der anzuzeigenden Beiträge in Ihrem WordPress-Admin-Panel-Einstellungen-Bereich unter der Registerkarte „Lesungen“ festgelegt. In diesem Artikel zeigen wir Ihnen jedoch, wie Sie diese Zahl mithilfe eines Super Loops überschreiben können, mit dem Sie eine beliebige Anzahl von Beiträgen in dieser bestimmten WordPress-Schleife anzeigen können. Auf diese Weise können Sie die Anzeige Ihrer Seiten anpassen, einschließlich Autorenprofilen, Seitenleisten und mehr.
Öffnen Sie eine Vorlagendatei, in der Sie die Beiträge platzieren möchten, und fügen Sie dann einfach diese Schleife hinzu:
// if everything is in place and ready, let's start the loop <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> // 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 <?php static $count = 0; if ($count == "n") { break; } else { ?> // 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 '<?php the_excerpt(); ?>' to display post excerpts instead <div class="post"> <?php the_title(); ?> <?php the_content(); ?> </div> // 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 <?php $count++; } ?> <?php endwhile; ?> <?php endif; ?>
Und Sie sind fertig. Dieser Code ist für Sie besonders beim Entwerfen der Autorenvorlage sehr hilfreich, da Sie die Anzahl der in jeder Schleife angezeigten Beiträge steuern möchten.