Understanding the Flow of the Loop and where it can be used

understanding the flow of the loop
Share this:

Flow of the Loop

The Loop uses some common programming conditional statements to work out what and the way to display. The primary statement within the Loop is an if statement, checking whether or not any posts exist, because you might not have any posts with the required category or tag.

If content exists, the while statement is used to initiate the Loop and cycle through all posts or pages that require to be displayed. Finally, the_ post() function is named to make the post data, creating it accessible to different WordPress functions.

Once the post content has been engineered, Loop content are often displayed in whatever format you wish.
Following could be a most minimal Loop example. This instance options the sole needed parts for the Loop
to function properly:

Remember that this can be PHP code, therefore it has to be enclosed in <?php and ?> tags. This can be the Loop in its simplest kind. If you’re questioning how the output from the database got two-handed to the current simple Loop once there aren’t any variables passed as parameters, the solution lies within the global variable $wp_query, that is an instance of WP_Query that’s documented by the functions within the easy Loop.

It is in impact the ‘‘default query’’ for the Loop. Note that by the time this default Loop is named, WordPress has already known as the geta posts()method among the default query object to make the list of acceptable content for the URL being viewed, and also the Loop during this case is charged with displaying that list of posts.

Later on, you explore a way to hand-structure queries to exercise fine-grain management over post selection, except for currently it’s safe to assume that the database work has been done, and also the results are stored in $wp_query, once the Loop is invoked.

Some terribly minimal necessities exist for the Loop to figure in WordPress. Let’s break down this example to appear at the various components of the Loop:

This line checks if any posts or pages are planning to be displayed on this page you’re viewing. If posts or pages exist consequent line can execute:

The preceding whereas statement starts the Loop, primarily iteration through all posts and pages to  be displayed on the page till there aren’t any additional. The Loop can continue whereas content exists to be displayed. Once all content has been displayed the whereas loop can finish. The statesman posts() function simply checks to ascertain if the list of posts being processed is exhausted, or had no entries to start with.

Next, the the_post() function is named to load all of the post content. This function should be known as within  your loop for the post content to be set properly. Calling post() successively calls the setup_postdata() function to line up the per-post data like the author and tags of the content you’re displaying in the Loop, additionally because the content of the post itself.

This data is assigned to a global variable whenever through the Loop iteration. Specifically calling the the_post() has the aspect impact of fitting the world $post variable employed by most of the model tags delineated anon, then advances to consequent post in the list.

Setting up the post content conjointly applies the suitable filters to the raw content that comes out of the WordPress database. WordPress stores user-edited content precisely as entered, therefore if a user adds a shortcode, for instance, to feature a Google AdSense item at the top of a post, the shortcode is hold on in the database content.

Once the post setup is completed, the plugin that converts that shortcode to a bit of JavaScript is named, in conjunction with different registered plugins that modify the raw post content. It’s vital to notice the excellence between the raw post content within the WordPress query object and also the filtered content that’s eventually rendered.

This is where all Loop model tags are placed and any further code you would like displayed within the Loop. we tend to cover this in additional detail.

The endwhile and endif calls end the Loop. Any code placed once these 2 lines can show at the bottom of your page, in any case posts are displayed. you may conjointly place an else clause to show a message if there’s no content to show within the Loop.

The Loop is typically enclosed by markup language tags in your theme model files. The subsequent code shows how the Loop is structured within the default kubrick theme that comes with WordPress:

Notice how the most minimal Loop parts exist, however are enclosed by markup language tags. This can be how a normal theme model file are going to be structured to utilize the Loop. The markup language parts will certainly modification, however the Loop parts keep an equivalent. Customizing the style during which content is displayed and selecting post data to incorporate within the page composition is completed through template tags.

What are your thoughts about loops in WordPress? Let me know in the comments!

Did you like this article about the loops in Wordpress? Share it with your friends on Facebook, Twitter, and Google Plus!

Don’t forget to Subscribe to our Newsletter for more insightful post updates, tips and guides!

See you in the next post.

Stay awesome!

Authored By Aakanksha Singh

Hi, I’m Aakanksha, a computer science engineer and the founder of TheTechPower. I help people make there online presence process easier by empowering them with clean, beautiful and easy to use wordpress themes.

Share this:

Leave a Reply