|
Post by account_disabled on Jan 31, 2024 8:22:23 GMT
本文是与SiteGround合作创建的系列文章的一部分。感谢您对合作伙伴的支持,使 SitePoint 成为可能。 当讨论 WordPress,特别是主题的开发(或在现有主题内创建新的页面模板)时,您不可避免地会遇到“循环”。Loop 是 WordPress 为用户访问的任何给定页面构建内容的框架,无论是静态主页还是显示最近帖子的博客视图,或者介于两者之间的任何页面。听起来可能有点复杂,但实际上,这只是一个循环机制。 最简单的循环是一个循环结构,就像编程中的任何其他结构一样。它会迭代网站所有内容的列表,循环浏览您的帖子或页面,并从中获取请求的内容。最复杂的是,您可以多次运行循环,仅获取特定类别中的特定项目、仅获取不属于特定类别的项目、在某个日期范围内发布的项目或具有其他特定识别信息的项目。 WordPress 主题中的每个页面模板都可能包含循环。这是模板从存储在数据库中的页面和帖子中搜索和获取内容的一种方式。让我们看一下一些具体内容: 您可以在上面的示例中看到,这确实是一个非常简单的设置。整个事情从一个条 WhatsApp 号码数据 件开始,have_posts检查以确保实际上有任何帖子可供查找。然后循环发生——当仍然有帖子时(再次使用have_posts),它迭代下一篇帖子并调用the_post——它指的是当前正在迭代的帖子。 特定查询 如果您的需求比简单地返回网站上的每个帖子更高级,则您需要限制查询。这就是 WP_Query发挥作用的地方。 按类别过滤 在下面的示例中,根据 Codex 中的示例进行修改,我们将查询 id 为 4 的类别中的帖子。然后,您可以看到循环本身内容的示例。 在这里,我们检查类别 ID 为 4 的帖子,然后在 div 中.post显示帖子的标题(链接到帖子)、帖子的日期、帖子的内容和帖子的元数据。请查看WP_Query文档。 提示:您可以通过多种方式获取类别的 ID 号。一种简单的方法是转到“帖子”,然后转到 WP 管理中的“类别”。右键单击列表中所需的类别名称,然后保存 URL。然后将其粘贴到文本编辑器或记事本中的某处并查看。例如,它可能看起来像这样您正在寻找号码tag_ID! 按自定义字段值过滤 Build Your Own Developer Portfolio WordPress 中提供的自定义字段非常有用,有时您可能还需要根据这些字段中设置的值来过滤帖子。这是一个简单的任务,但值得向 WordPress 新手开发人员(或那些没有深入研究自定义模板或循环的人)展示,以了解这一切的可定制性。
|
|