WordPress Theme Development Part 8 – The Loop

Loop is a php code that display WordPress Posts. Posts is a single article. In a newspaper website we read a single news. That is a post. In WordPress dashboard you can see Posts option under the Dashboard menu. You can Add new post with post categories.

In my Travel theme I want a Travel Blog. Several category of travel posts will be there. So I will add Categories then add posts of that categories. Now If I want to display these posts in my theme then I have to use WordPress loop.

The Loop

Loop starts with this code.


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

and ends with


<?php endwhile; ?>
<?php endif; ?>

This code check if there is any posts already in WordPress then run a While loop. In between this loop we can display our Post Title, Post Content , Date, Post link and many more. Here is some example of items that you can show by post loop.


<?php the_title(); ?>
<?php the_content(); ?>
<?php the_permalink(); ?>
<?php the_content(); ?>
<?php the_time( 'F jS, Y' ); ?>
<?php the_category( ', ' ); ?>
<?php the_author_posts_link(); ?>

Custom Post Loop

We will discuss more about Custom Post later but for now just some idea about how to use loop on it. Custom Post Type is same as WordPress post with different type content. Internally custom post store in a same place in database. Here is a sample loop that you can use for looping custom post.


<?php $query = new WP_Query( 'post_type=travel' ); ?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<?php endwhile; ?>
<?php endif; ?>

Thanks for reading !

 

Leave a Reply

Your email address will not be published. Required fields are marked *