了解WordPress中的“循环”

Loop

This article is part of a series created in partnership with SiteGround. Thank you for supporting the partners who make SitePoint possible.

本文是与SiteGround合作创建的系列文章的一部分。 感谢您支持使SitePoint成为可能的合作伙伴。

When discussing WordPress, and specifically the development of themes (or creating new page templates inside an existing theme) you inevitably will run up against “The Loop”. The Loop is the framework within which WordPress constructs the content for any given page that user visits, whether that be a static home page or a blog view showcasing recent posts, or anything in between. It may sound a bit complex, but really, it’s just a looping mechanism.

在讨论WordPress,特别是主题的开发(或在现有主题内创建新的页面模板)时,您不可避免地会遇到“ The Loop”问题。 Loop是一个框架,WordPress可以在该框架内为用户访问的任何给定页面构造内容,无论是静态主页还是显示最新帖子的博客视图,或介于两者之间的任何内容。 听起来可能有点复杂,但实际上,这只是一个循环机制。

The Loop, at its simplest, is a looping structure just like any other in programming. It iterates through what amounts to a list of all of your site’s content, cycling through your posts or pages, and fetching the requested content from them. At its most complicated, you can run The Loop multiple times, fetch only certain items from certain categories, only items not in certain categories, those published within a date range, or with other particular identifying information.

循环最简单地是一种循环结构,就像编程中的任何其他循环一样。 它遍历整个站点内容列表,循环遍历您的帖子或页面,并从中获取请求的内容。 最复杂的是,您可以多次运行The Loop,仅从某些类别中获取某些项目,仅获取不在某些类别中的项目,在日期范围内发布的项目或其他特定的标识信息。

Every page template within a WordPress theme will likely contain The Loop. It’s one way that the template can search for and acquire content from your pages and posts, which are stored in the database. Let’s take a look at some specifics:

WordPress主题内的每个页面模板都可能包含The Loop。 模板是从存储在数据库中的页面和帖子中搜索和获取内容的一种方法。 让我们看一些细节:

循环的基本示例 (A Basic Example of the Loop)

<?php 
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        // Post Content here
    }
}
?>

You can see in the above example that it’s really a pretty straightforward setup. The entire thing starts with a conditional, with have_posts checking to ensure that there are, in fact, any posts to find. Then the loop happens – while there are still posts (again using have_posts), it iterates through the next post and calls up the_post – which refers to the one currently being iterated through.

您可以在上面的示例中看到它确实是一个非常简单的设置。 整个过程以有条件的开始,首先进行have_posts检查,以确保实际上找到了任何帖子。 然后,发生循环- 仍然有帖子(再次使用have_posts )时,它循环遍历下一个帖子并调用the_post指的是当前正在迭代的帖子。

特殊查询 (Particular Queries)

If your needs are more advanced than simply returning every post that there is on your website, you’ll need to limit your queries. This is where WP_Query comes into play.

如果您的需求比仅返回网站上的每个帖子都更高级,则需要限制查询。 这就是WP_Query起作用的地方。

按类别过滤 (Filtering by Category)

In the below example, modified from an example in the Codex, we will query for posts that are in the category which has the id of 4. Then, you can see a sample of the contents of the loop itself. Here, we check for posts with a category ID of 4, then, within the .post div, we display the post’s title (linked to the post), the date of the post, the post’s content, and the post’s metadata.

在下面的示例(从Codex中的示例进行了修改)中,我们将查询ID为4的类别中的帖子。然后,您可以看到循环本身内容的示例。 在这里,我们检查类别ID为4的帖子,然后在.post div中显示帖子的标题(链接到帖子),帖子的日期,帖子的内容以及帖子的元数据。

<!-- Query for posts which are in category 4 -->
<?php $query = new WP_Query( array( 'cat' => 4 ) ); ?>

<!-- Begin The Loop -->
<?php if ( $query->have_posts() ) {
      while ( $query->have_posts() ) {
        $query->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>

                         <!-- Display the post content -->
            <div class="entry">
                  <?php the_content(); ?>
            </div>

                        <!-- Display the post metadata -->
            <p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
        </div> 
    }
}

The use of WP_Query can lead to some very customizable results. You can include posts of only one category, or several, or include all except for those from one category or another. You can search and return posts that contain a keyword, or locate posts by ID, use the post_type to show only data from pages, and more. If you want to learn in more depth about WP_Query, take a look at the WP_Query documentation.

WP_Query的使用可以导致一些非常可定制的结果。 您可以仅包含一个类别或多个类别的帖子,也可以包含一个类别或另一类别的帖子以外的所有帖子。 您可以搜索和返回包含关键字的帖子,或按ID查找帖子,使用post_type仅显示页面中的数据,等等。 如果您想更深入地了解WP_Query ,请查看WP_Query文档。

Tip: You can get the ID number of a category in several ways. One easy way is to head over to “Posts”, then “Categories” in your WP-Admin. Right click on the desired category name in the list, and save the URL. Then paste it in a text editor or notepad somewhere and take a look. As an example, it might look something like this: http://example.com/wp-admin/term.php?taxonomy=category&tag_ID=4&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory – you’re looking for the tag_ID number!

提示:您可以通过多种方式获得类别的ID号。 一种简单的方法是转到WP-Admin中的“帖子”,然后转到“类别”。 右键单击列表中所需的类别名称,然后保存URL。 然后将其粘贴到文本编辑器或记事本中的某个位置并进行查看。 例如,它可能看起来像这样: http://example.com/wp-admin/term.php?taxonomy=category&tag_ID=4&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory : http://example.com/wp-admin/term.php?taxonomy=category&tag_ID=4&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory - http://example.com/wp-admin/term.php?taxonomy=category&tag_ID=4&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory - http://example.com/wp-admin/term.php?taxonomy=category&tag_ID=4&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory –您正在寻找tag_ID号!

按自定义字段值过滤 (Filtering by Custom Field Values)

The custom fields that are available in WordPress can be fantastically useful, and you may at times also need to filter posts by the values set in those as well. A simple task, but it’s worth showing for beginning WordPress developers (or those who haven’t delved far into custom templates or The Loop) to realize how customizable all of this is. Say that you have a custom field department and you’re looking for posts with the value marketing:

WordPress中可用的自定义字段可能非常有用,并且您有时有时还需要根据其中设置的值来过滤帖子。 一个简单的任务,但值得初学者WordPress开发人员(或尚未深入研究自定义模板或The Loop的开发人员)展示,以实现所有这些功能的可定制性。 假设您有一个自定义的现场department并且正在寻找具有价值marketing职位:

$query = new WP_Query( array('meta_key' => 'department', 'meta_value' => 'marketing') );

了解“循环” (Understanding “The Loop”)

The truely best way to understand The Loop is simply to use it. Use it to fetch your content in the templates you create for themes, or in the code that you customize in existing templates in existing themes. When you want to do something, filter for certain criteria, run The Loop again – check the The Loop documentation in the Codex, or hit up Google for ideas. The sky’s the limit!

理解The Loop的真正最佳方法就是使用它。 使用它可以在为主题创建的模板中或在现有主题的现有模板中自定义的代码中获取内容。 如果您想做某事,请过滤某些条件,然后再次运行“循环” –查看食典中的“循环”文档 ,或访问Google寻求思路。 天空是极限!



If you’re looking for somewhere to host your WordPress site after you’ve got your templates built and The Loop all figured out, take a look at our partner, SiteGround. They have affordable WordPress hosting available, with one click installation, staging environments, and more!

如果您在构建完模板并且找到了The Loop之后想要在某个地方托管WordPress网站,请查看我们的合作伙伴SiteGround 。 他们提供价格合理的WordPress托管服务,并提供一键安装,登台环境以及更多功能!

翻译自: https://www.sitepoint.com/understanding-loop-wordpress/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值