Categories
Wordpress Wordpress Loop

WordPress the loop | Complete Tutorial with List of Best Loop Tutorials

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.

 

By Anirudh Babbar

A website developer, loves #wordpress #drupal #php. Love posting about wordpress, php, bootstrap and SEO. Also provide online/offline training on wordpress, drupal, php and SEO. I am available on Twitter @ anibabbar , Skype @anibabbar , gmail @anibabbar !! facebook @thinkncode.com