为什么我需要在 single.php 中使用 wp 循环 [英] Why i need a wp loop in single.php

查看:28
本文介绍了为什么我需要在 single.php 中使用 wp 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WordPress主题开发中,我们可以使用single.php来显示特定的单个帖子.

In WordPress theme development we can use single.php to show the specific single post.

为此,通常的做法是:

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

为什么我需要循环显示单个帖子?谁能给出一些合理的理由?

Why do I need looping to show a single post? Can any one give some valid reason?

推荐答案

WordPress Loop 实例化了一些函数像 the_title()the_content()其他人.

The WordPress Loop instantiates some functions like the_title(), the_content() and others.

换句话说,您的帖子在该循环中加载,如果您在单个帖子上,则循环会经过一次.虽然有一个循环可能很奇怪,但它实际上很有用.

In other words, your post is loaded in that loop, and the loop is gone through once if you are on a single post. Although it might be strange to have a loop, it is actually quite useful.

WordPress 使用 模板层次结构,这是一种选择为给定帖子加载哪个模板的方法/页.在您的 single.php 中,循环将运行一次.但是,如果您没有 single.php 文件,WordPress 将使用 index.php 代替同一帖子.

WordPress uses a template hierarchy, which is a way of choosing which template to load for a given post/page. In your single.php, the loop will run once. But if you do not have a single.php file, WordPress will use index.php instead for that same post.

为了保持一致性,有一个适用于任意数量帖子的循环是有帮助的.否则,在您的 index.php 中,您将需要一个帖子的案例和多个帖子的另一个案例,并且很难保持一致的模板方法.

For the sake of consistency, having a loop which works for any number of posts is helpful. Else, in you index.php, you would have needed a case for one post and another case for multiple posts and keeping a consistent templating method would be difficult.

这篇关于为什么我需要在 single.php 中使用 wp 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆