开发者

PHP Re-create Wordpress the_loop

开发者 https://www.devze.com 2023-03-29 12:26 出处:网络
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 r

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开发者_如何学Go 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!


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:

$iterator = new PostIterator($category, $page);
while ($iterator->hasNext()) {
   $post = $iterator->next();
   echo $post->title . ' ' . $post->getFormattedDate() . '<br />';
}


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:

function the_title() {
    global $articles;
    static $position;

    return $articles[$position++]->title;
}

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 like the_post().


I changed current_article to a public and not a static 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消