Beginner’s Guide To WordPress Loop ![rw-book-cover](https://www.cloudways.com/wp-content/themes/cw_multilingual/template/assets/img/favicon.png) ## Metadata - Author: [[The Official Cloudways Blog]] - Full Title: Beginner’s Guide To WordPress Loop - Category: #articles - Summary: The code that displays the blog posts on your WordPress theme is widely known as the WordPress Loop among WordPress users and as The Loop on WordPress codex. The Loop is considered as the most important part of WordPress theme. - URL: https://www.cloudways.com/blog/beginners-guide-wordpress-loop/ ![](https://www.cloudways.com/blog/wp-content/uploads/WordPress-Loop.jpg) Some content could not be imported from the original document. [View content ↗](https://www.facebook.com/v3.2/plugins/like.php?action=like&app_id=&channel=https%3A%2F%2Fstaticxx.facebook.com%2Fx%2Fconnect%2Fxd_arbiter%2F%3Fversion%3D46%23cb%3Df13e2009ffef9e4%26domain%3Dwww.cloudways.com%26is_canvas%3Dfalse%26origin%3Dhttps%253A%252F%252Fwww.cloudways.com%252Ff3662ee0e96384c%26relation%3Dparent.parent&container_width=0&href=https%3A%2F%2Fwww.facebook.com%2Fcloudways%2F&layout=button_count&locale=en_GB&sdk=joey&share=false&show_faces=true&size=small) Some content could not be imported from the original document. [View content ↗](https://platform.twitter.com/widgets/follow_button.644279d1635fd969e87af94a98bd232b.en.html#dnt=false&id=twitter-widget-0&lang=en&screen_name=Cloudways&show_count=true&show_screen_name=true&size=m&time=1672500716275) The code that displays the blog posts on your WordPress theme is widely known as the WordPress Loop among WordPress users and as The Loop on [WordPress Codex](https://codex.wordpress.org/The_Loop). The Loop is considered the most important part of the WordPress theme. It is responsible for displaying blog posts on the current page based on the parameters. In this guide, we will go over the basics of WordPress Loop with examples so that you get a clear understanding of what the code does, how to customize it, and where it can be found in your WordPress theme. #### What Is WordPress Loop? A PHP code that displays WordPress posts is called a loop or WordPress loop. WordPress themes use a loop to display the posts on the current web pages. Loop is based on some functions designed to display the posts by running these functions. WordPress Loop is one of the most important elements of WordPress code, with several Template Tags that are used to publish, format, and arrange post data. The Loop displays the following information by default for each post: * Title (the\_title()) * Time (the\_time()) * Categories (the\_category()). #### The Logic of WordPress Loop To start off with the logic, let’s have a look at the basic code of the loop. Then we can go over each line to understand it. In the Loop, we have one “if” statement and a “while” loop. The “if” statement checks if there are posts available to be displayed, the “have\_posts” WordPress function is a boolean one, that returns a true or false value. If there are posts found, it returns “true” and code proceeds to “while” loop. The “while” loop also has the “have\_posts” WordPress function. It returns “true” equal to the number of times we have set blog posts to be displayed in the WordPress admin. The code then proceeds to the WordPress function “the\_post”. This function sets up the post and then we can use more functions to extract elements from the posts. In the above-stated example we have used: the\_title -> fetches the post title the\_post\_thumbnail -> fetches the featured image of the blog post the\_excerpt -> fetches the excerpt of the blog post There are certainly more functions that we can use to extract elements from posts, we will cover a few examples. #### Hyperlinking on Post Title You have probably seen that blog post titles have links to the actual posts. Upon clicking on the hyperlink you are navigated to a single page view of the blog post. Most themes have a file called single.php, this file is called when you are viewing a single post. We will use the “the\_permalink” WordPress function and enclose the “the\_title” function to link it. Example: #### Displaying the Content, Author, Date, and Category Using the Loop You might have seen many blogs with posts that show the name of the author, entire content of the main blog page, with date and category also. You can easily display all these useful details within the loop using the following functions: the\_content – Displays the full blog content then\_author – Displays the author-name the\_time – Displays the time and date the\_category – Displays the category of the post Your loop will look something like #### Use of Conditional Tags There are many tags used to check for conditions, but fortunately, in WordPress, these tags are pretty much self-explanatory. For example, “is\_home” checks whether the current view is of the home page (main page of the blog). We can use [Conditional Tags](http://codex.wordpress.org/Conditional_Tags) to initiate certain Loops. For example, if we enclose our Loop between “if” condition that checks whether the current page is the main page, then the Loop will only be executed on the main page. We can use Conditional Tags in our template files to control which Loop is executed, thus having multiple loops in a single file. Some of the most used Conditional tags are. is\_admin() – Returns true when admin is logged in the site. is\_single() – Returns true if single post is being viewed. is\_page() – Returns true on certain pages, e.g. is\_page(‘about-us’) is\_category() – Returns true on category pages, e.g. is\_category(‘wordpress’) is\_tag() – Returns true on tags. is\_author() – Checks for author and Returns true. e.g. is\_author(‘ahsan’) is\_404() – Returns true if the page does not exist, can be used on 404 pages to execute a customized Loop has\_excerpt() – Returns true if a post has an excerpt. #### 5 Useful Loops in WordPress Let’s have a look at five common loops in WordPress. ##### 1. Insert Ads After the First Post If you need to place Ads after the first post, this is a way to do this. Just paste your Ad code and the ad will be placed after the first post. ##### 2. Fetch Posts Between Two Dates One of the common use cases is to fetch posts for a specific time period. To do so, use the WordPress loop like so. Inside the code, we use an SQL function that contains the WHERE clause. The function filter\_where is then hooked to **post\_where()** function. ##### 3. List Upcoming Posts A list of upcoming posts is a smart way of keeping the visitors busy on your blog. This can be achieved through the following loop. In the above code, we use the post\_status function to fetch posts according to their published date. ##### 4. Create An Archive Page The archive page is a great way of listing your best posts from the past so that readers can still find them on your blog. Make sure to create a template page to display archived posts. ##### 5. Create Image Loop The image loop is a great way of showing your blog posts in a gallery-like display. Each post is displayed with an image and its excerpt. Let’s see how to do that using WordPress Loops. To display the images on a browser, add the following code. WordPress **have\_posts** function is used to check if any data is available to loop. It returns true if the data is available. #### The Loop is the Future Think of the Loop as the driving engine of [WordPress themes](https://www.cloudways.com/en/best-wordpress-themes). It is necessary to use it to display posts. This blog explains the basics of using a loop, but I recommend you go over [the Loop in action](https://codex.wordpress.org/The_Loop_in_Action) to discover more ways of using it. But, the Loop would work great if it is coupled with a high-performing [fast WordPress hosting](https://www.cloudways.com/blog/fastest-wordpress-hosting/) medium. [Cloudways](https://www.cloudways.com/) is ready for the Loop! Our tests have shown that websites on Cloudways load 100% faster. ##### Q. What is the WordPress Loop? WordPress Loop is a technique of displaying a list of data dynamically with specified parameters. ##### Q. Types of loops in WordPress? There are several types of loops, a user can create a loop as per his own requirements using the pre-defined WordPress functions. ##### Q. How to Display Any Number of Posts in a WordPress Loop? To do so, you need to limit the number count while running the loop around WordPress posts. Customer Review at ###### “Beautifully optimized hosting for WordPress and Magento” Arda Burak [Agency Owner] #### THERE’S MORE TO READ. [![](https://www.cloudways.com/blog/wp-content/uploads/how-to-update-wordpress-t.jpg)](https://www.cloudways.com/blog/how-to-update-wordpress-site/) [![](https://www.cloudways.com/blog/wp-content/uploads/Check-Upgrade-to-the-Latest-WordPress.jpg)](https://www.cloudways.com/blog/wordpress-version/) [![](https://www.cloudways.com/blog/wp-content/uploads/WordPress-Multisite-Plugins-Thumb.jpg)](https://www.cloudways.com/blog/wordpress-multisite-plugins/), Beginner’s Guide To WordPress Loop, The Official Cloudways Blog, 01gnmcyzd0e9wwfxg25j7dynak.