Wordpress

WordPress Posts Loop Explained in Detail

5D5KSUVUV7AD Ever thought that whenever you publish a post using the amazing blogging platform WordPress then after clicking publish you can see your post at the homepage of your blog. Now how WordPress posts loop show up at the home page? and how your content is basically called at the front page of the website. I am going to explain the basic code which runs behind the theme of your WordPress blog and shows the posts at the specified area.

Before reading this more advanced post you should also read the previously posted WordPress posts:

Understanding the WordPress basics – A popular Blogging CMS

Installing WordPress on computer/windows with xampp

After you have read the previously posted articles you should have learnt that how can you install wordpress on your computer so easily. After installing the wordpress now you can play around with the knowledge shared in this post.

WordPress Posts Loop

What is basically WordPress posts Loop?

As I explained previously in the start of this post that a WordPress posts loop is something which is called to get all the published posts. It’s called as Loop because it is a recurring process to call all the posts or the number of posts desired. But Today we are not going to modify the WordPress posts loop instead I am just going to explain the very basic Loop of WordPress posts. It’s actually a php query code which can used to call your posted content through the database where all the posts contents in the form of text is saved.

How To: Use WordPress posts Loop in your Template:

It’s much easy and can be done in a matter of seconds. Here at this stage you need to grab a good editing software for code files like notepad++. After having a syntax highlight based text editor go to your WordPress installation and navigate to:

/wp-content/theme/index.php – Edit this file

Here I am assuming that you want to show your posts at the homepage of your blog. Thus copy the below code and add it to your index.php file:

 <!-- Start WordPress posts loop - the Loop. -->
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- calling the post title -->
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- calling the published date time -->
 <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
 <!-- Display the Post's content in a div box. -->
 <div class="post-tow-content">
   <?php the_content(); ?>
 </div>
 <!-- Display a comma separated list of the posts Categories -->
 <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
 <?php endwhile; endif; ?>

You can already notice that such a code is already present in your index.php file and if not then you can add the code given above. However If already present then just move on to the next step to understand what exactly the WordPress posts loop does.

How this WordPress posts Loop Work to Call the posts and it’s format i-e Title, summary etc.

This is the basic point of my post to make you learn that how the WordPress uses the php to call the posts and their meta data as per your desire to show up at any desired area of your blog using the Loop. You can easily understand the loop given above because of the fact that I commented out every section in the green. However Again let me explain to you!

The Following code starts the Loop and check that if there are any published posts at your WordPress blog? If yes then it calls for those posts but follows the conditions given below in the code. So here is the first part:

<!-- Start WordPress posts loop - the Loop. -->
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Second  Part Calls for the post title in the heading 2 (h2) format.

<!-- calling the post title -->
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

Third part Calls the time at which the post was published from the database.

<!-- calling the published date time -->
 <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>

Fourth part calls for the total content of the post.

 <!-- Display the Post's content in a div box. -->
 <div class="post-tow-content">
   <?php the_content(); ?>
 </div>

Fifth part just calls the name of the category with the link in which you have filed the particular post.

<!-- Display a comma separated list of the posts Categories -->
 <p class="postmetadata">Posted in <?php the_category(', '); ?></p>

In the end, the loop is just stopped.

<?php endwhile; endif; ?>

2 Comments