The Ultimate Guide to WordPress Loop

What is The Loop?

You’re probably still wondering what The Loop even is. Basically,it’s what displays the content you see on your homepage,your singlepostspages,archives, search results, and more.

If a user accesses your homepage, archives, or search results – bydefault, the Loop will display a certain number of posts as definedin your Reading Options.

WordPress Reading Options

At the moment, my homepage displays10 posts per page, which is what I defined Showat most * posts. On single posts and pages – the samebasic Loop code will just display just thatspecific page.

Basic flow of the loop

Let’s break the Loop down into 3 parts.

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

1. What you want displayed in the Loop

<?php endwhile;?>

2. What is displayed when the Loop is over

<?php else : ?>

3. If there’s nothing to display

<?php endif; ?>

If there are postsavailable in the query, it will start displaying them ina while loop,what is defined in part1. When the while is over, it will display what isin part2. If there’s no posts found, or there’s some sort ofother 404 error, part3 getsdisplayed.

Template Tags used within the Loop

Unless you want 1.What you want displayed in the Loop repeated on yourWordPress blog’s homepage 10 times, you should probably learn someof the basic template tags. Let’s take a look at the codeof index.php inthe default WordPress template.

WordPress Loop Breakdown

As you can see, there’s quite a few templatetags within the Loopthat will output things such as the post title, the permalink, thecontent, etc. I’ll break down each of the template tags in theWordPress default theme.

  • <?php the_permalink()?> – This will echothe permalink of the post, 
  • <?php the_title();?> – This echos thepost title, i.e.Hello World!
  • <?php the_time(‘F jS, Y’)?> – This will echothe date, i.e.April 4th, 2008. A full list of ways to format the date can befound on php.net
  • <?php the_author()?> – This willdisplay the author’s name, i.e.Leland. This is commented out in the default theme.
  • <?php the_tags(‘Tags: ‘, ‘, ‘, ‘<br/>’); ?> – This willdisplay the tags assigned to the post, separated by commas, andfollowed by a line break
  • <?php the_category(‘, ‘)?> – This willdisplay the categories in a similar fashion as the tags above.
  • <?php edit_post_link(‘Edit’, ”, ‘ | ‘);?> – The edit postlink will be visible only to those with permission.
  • <?php comments_popup_link(‘No Comments »’, ’1Comment »’, ‘% Comments »’); ?> – Will display thelink to the comments. This will not be displayed on single posts orpages.

There are a lot more listed on the TemplateTags page over atWordPress.org. Some of these may work in the Loop, while some maynot.

After the Loop

Let’s take a look at the code after the loop stops looping in thedefault theme.

After the Loop breakdown

<div class="navigation">
<divclass="alignleft"><?phpnext_posts_link('&laquo; Older Entries')?></div>
<divclass="alignright"><?phpprevious_posts_link('Newer Entries &raquo;')?></div>
</div>

As you might have guessed, this will display the pagination you seeon the homepage, archives, and search results. These won’t bedisplayed on single posts and pages. Of course you could replacethis with something like PageNavi,but that’s up to you.

If there are no posts to display (possibly due to a 404 error), thefollowing will be displayed after the else

<h2 class="center">NotFound</h2>
<p class="center">Sorry, but you arelooking for something that isn'there.</p>
<?php include (TEMPLATEPATH . "/searchform.php");?>

That will display the NotFound message along withthe search form. In this case there would have to be a search formcode located in a file called searchform.php inthe template directory, which there is in the default theme.

Template Hierarchy

Some template files will take priority over the index.php forcertain types of pages if they are present in the templatedirectory. Listed below are a few examples of template hierarchy,listed in order of priority.

Homepage
  1. home.php
  2. index.php
Single Post
  1. single.php
  2. index.php
Search Results
  1. search.php
  2. index.php
404 Page
  1. 404.php
  2. index.php

There are a few more advanced techniques listed on the TemplateHierarchy page over atWordPress.org.

So what’s the point of the template hierarchy? Basically you canuse the it to create new layouts for different types of WordPresspages without hacking up your index.php filetoo much.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值