PHP 重新创建 WordPress the_loop
我目前正在开发一个基本的定制 CMS,我们将在我们的多个网站中使用它。在有人攻击我之前,我很清楚其他选择,但是我们尝试过的任何方法对于我们的数据来说都不够灵活。
抛开这个问题,我要开始了。
我喜欢 WordPress 的功能之一是“循环”。
while(have_posts()) : the_content();
the_content();
endwhile;
我研究了代码并提出了一个类似的类,您可以在此处查看。
通过查看代码,我发现,如果数组中仍然有帖子, has_posts()
似乎会返回一个布尔值。
the_article
表示我们仍在循环中,因此使用我们需要的数据为文章(帖子)设置一个变量。
我已经完成了这项工作,但是我似乎只能从数组中获取一条信息:
while (have_articles()): the_article();
echo "<h1 class='title'>" . the_title() . "</h1>";
endwhile;
其中 the_title
是:
function the_title() {
global $AC;
return $AC->p_title;
}
感谢您的帮助!
I'm currently working on a basic, but bespoke CMS that we will be using across several of our sites. Before anyone flames me, I am well aware of other alternatives, however nothing we have tried is really flexible enough for our data.
With that out of the way, I shall begin.
One of the features I do like from Wordpress is the known as The Loop.
while(have_posts()) : the_content();
the_content();
endwhile;
I've studied the code and come up with a similar class, which you can see here.
From looking at the code, I've figured out that, has_posts()
seems to be returning a boolean if there are still posts in an array.
the_article
is saying that we're still in the loop, so set a variable for the articles(posts) with the data we need.
I've kind of got this working, however I only seem to be able to get one piece of information from the array:
while (have_articles()): the_article();
echo "<h1 class='title'>" . the_title() . "</h1>";
endwhile;
Where the_title
is:
function the_title() {
global $AC;
return $AC->p_title;
}
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有什么比 WordPress 更适合借用概念和实现了。
当你试图理解系统各部分如何协同工作时,循环是最糟糕的事情。循环引入了大量全局变量,这些变量仅在循环内有效,但仍然可以从循环外部访问。没有人知道这些变量是否以及在哪里可以有效使用。
以 OOP 方式实现您自己的“循环”。永远不要试图理解和复制 WordPress 代码。很糟糕。
我希望帖子的迭代看起来像这样:
There is probably nothing that is that much unqualified to borrow concepts and implementations than from wordpress.
The Loop is the worst thing you can have when trying to understand how the system parts work together. The Loop introduces a vast number of global variables who are valid only within the loop but are still accessible from outside the loop. No one knows if and where these variables are valid to be used or not.
Implement your own "loop" in an OOP manner. Do not ever try to understand and copy wordpress code. Its bad.
How I would expect an iteration over the post to look like:
有趣的是,我认为循环是 WordPress 中最糟糕的部分。太多的魔法毫无意义。
无论如何,要实现循环,就像:
看,每次调用
the_title()
等时,都必须增加一个指针。如果您想在the_post()
等其他函数中使用它,您可能还必须将$position
设为全局。Funny, I see the loop as the worst part of Wordpress. Too much magic for nothing.
Anyway, to implement the loop, it's something like:
See, each time you call
the_title()
and the like, you have to increment a pointer. Probably, you will have to make$position
global as well, if you want to use it in other functions likethe_post()
.我将
current_article
更改为public
而不是static
值,这很有帮助。事实证明,我的函数the_title
访问了错误的字段名称。这个问题就解决了。
@所有回答的人;你的意见很有价值,我同意,循环是有问题和东西的,但它也提供了一种简单的方式(当你在控制中时)让内容轻松地变成你想要的样子。 请参阅 WordPress 主题。
I changed
current_article
to apublic
and not astatic
value, which helped. It also turned out that my function,the_title
was accessing the wrong field name.This problem is solved.
@Everyone who answered; your opinions are valued, and I agree, the loop is with its problems and stuff, but it also provides an easy (when you're in control) way of causing content to how you want it, easily. See Wordpress Themes.