Do you want better
IT Services ?

Let our experts help you navigate the digital world with tailor-made IT Solutions!

Table of Contents

Loop means repetition of data until the given condition is true.

In wordpress settings panel we have a option to display the number of posts. Check the image below.

 

Blog post show at most : 10 Posts

By putting the number 10 in the settings panel, we give instruction to repeat process ( show posts in this case) until the figure reaches 10. Above I defined Loop as, repetetion of data, until the condition is true. So in our case, till the number of post reaches the 10, the condition is satisfied and the loop continues and after it stops.

I hope you have got a good overview of loop till now, so we can move next to wordpress loop and how to code it properly in index.php.

WordPress The_Loop

In next few minutes you will be fully understanding the wordpress loop structure, excited!! ?, well you must be. Follow the tutorial and you will be done in few minutes. WordPress loop starts this way.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

and ends this way

<?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?> 
<?php endif; ?>

Everything entered in between starting and ending of loop will be repeated until the condition is true. Lets add the post title, post content and post thumbnail in the loop, to make the loop fully functional.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 <div class="post">

 <!-- Display the Title as a link to the Post's permalink. -->

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

 <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->

 <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>

  <div class="entry">

    <?php the_content(); ?>

  </div>

  <p class="postmetadata">Posted in <?php the_category(', '); ?></p>

 </div> <!-- closes the first div box -->

 <?php endwhile; else: ?>

 <p>Sorry, no posts matched your criteria.</p>

 <?php endif; ?>

This is it, this is all about wordpress loop. The div classes entry, post, postmetadata are pre defined wordpress classes, that helps us to properly format the output.

Multiple Loops

Alright we have coded the loop correctly, but we want more loop on index.php, let us say we need to display posts from category ‘featured’ . So how to do that ?, how to make a custom query to display only posts from a certain category, below i am mentioning the code required to do so.

<?php $new_query= new WP_Query('category_name=featured&posts_per_page=5'); ?>

<?php while ($new_query->have_posts()) : $new_query->the_post(); ?>

  <!-- Do special_cat stuff... -->
<?php the_title(); ?>

<?php the_content(); ?>

<!-- Add anything you want in this loop above this line --> <?php endwhile; ?>

What we did here is, we created a new variable $new_query and inserted a new WP_Query into it and gave it two parameters, first show posts from category : featured (category_name=) , and then limited posts per page to 5 (posts_per_page=5). We can change these two parameters on our own wish, to show posts from different categories.

One more important thing to note here is , we can only use [ if ( have_posts() ) : while ( have_posts() ) : the_post(); ] once in a page and if we need to make a another loop, we have to do it by defining custom variables, like the one we did above. Although we can create as many custom loops in a page as we want by just changing the variable name($new_query) each time. I hope you have learned something good about WordPress loop from this post.  Well, I am Anirudh Babbar from India (Delhi) and I give WordPress, Drupal, PHP training through Online Audio/Video Conferencing. If you have any sort of doubts or want to take online training you can contact me anytime at anibabbar@gmail.com or call at  09540491178 / +91-9812430938.

Below are the best tutorials online available for learning more about WordPress loop.

 

WordPress vs A Custom based php Website

WordPress and a custom PHP website are two different approaches to building a website. Here are some differences between the two: Ease of use: WordPress is generally easier to use than a custom PHP website, because it has a user-friendly interface and a large number of plugins and themes that allow you to easily add...

Read More

10 ways to bring targeted audience to your website

Search engine optimization (SEO): Optimizing your website for search engines can help bring targeted traffic from people searching for keywords related to your business. Pay-per-click (PPC) advertising: PPC advertising platforms like Google AdWords allow you to create ads that appear at the top of search engine results pages when people search for specific keywords. Content...

Read More

Top 8 Benefits of Having a Website

Top 8 Benefits of Having a Website Whether you’re a small business or a large corporation, having a website is essential to your success. In today’s digital world, people are used to getting their information online, and if you don’t have a website, you’re missing out on a huge opportunity. A website allows you to...

Read More

What is web hosting and why do I need it ?

Unless you are planning to build a website from scratch, you will need web hosting. Web hosting is a service that provides you with the technology and resources necessary to build and maintain a website. Think of it this way: if a website is like a house, then web hosting is like the land it’s...

Read More

How To Find The Right Website Designer For Your Business

A website is the face of your business – so it’s important that you take the time to find a designer who will create something that accurately represents your company. In this blog post, we’ll explore the process of selecting a designer, what you should keep in mind when hiring one, and how to go...

Read More

A Guide To Why You Need A Dynamic Website

The world has changed. The way people search for things have changed. The way they use technology to do their work have changed. Gone are the days when you create a website and take it as it is, customers will want more information, they’ll want better content, so you need to be ready to provide...

Read More
Scroll to Top
Scroll to Top

We help businesses with Websites, Mobile Applications, Digital Marketing, E Commerce Solutions & Custom Software.