WordPress Iteration in Your Own PHP Application
Wordpress has its own way to loop the posts, that is different than what other CMS does. I think it’s great because of flexibility and usability reason. It would seem easy later, when you want to manipulate the result before it is displayed to the browser.
So you want to implement the WordPress-style loop in your own PHP application? You have to say yes. ![]()
The following PHP code contains some basic functions to make your own WordPress Loop.
Code
Wordpress-style Loop
[raw]<pre>
$posts = mysql_query('YOUR SQL QUERY HERE');
$post = null;
$post_count = 0;
$post_index = 0;
function have_post() {
global $posts, $post_count, $post_index;
if ($posts && ($post_index < = $post_count)){
$post_count = count($posts);
return true;
}
else {
$post_count = 0;
return false;
}
}
function the_post() {
global $posts, $post, $post_count, $post_index;
// make sure all the posts haven't already been looped through
if ($post_index > $post_count) {
return false;
}
// retrieve the post data for the current index
$post = $posts[$post_index+1];
// increment the index for the next time this method is called
$post_index++;
return $post;
}
function the_title() {
global $post;
return $post->Title;
}
function the_content() {
global $post;
return $post->Content;
}
//and the output... tada....
if(have_post()) : while(have_post()) : the_post();
echo "
<h2>".the_title()."</h2>
";
echo the_content();
endwhile; endif;
</pre>
<p>[/raw] The advantage of using this iteration is:
1. Easy to implement with templating system.
2. Easy to filter output through PHP function, no need edit the template file.
3. Every output element is a function rather than a variable. It accepts parameter, you can do more things.
4. Want to display next post or previous post? that’s no a pain.
Disadvantage:
Of course, that code confusing me at the first time ![]()
Incoming search terms:
- wordpress post_count
- pengganti mysql_fetch_array
- apa fungsi while loop
- if not have_post wordpress
- menjalan function didalam if di php
- fungsi while loop dalam php
- coding loop dalam loop
- while dalam php
- while do di dalam if php
- wordpress $post_count
- http://www.kacanghijau.com Kacang Hijau
- http://fauzie.rofie.name/ Fauzie
- dholie
- dholie
- dholie
- iroel
- http://www.facebook.com/prahastu Prast Rahastu
- http://www.register-web-domain.in Domain register
- http://realistiksismebebek.posterous.com/ sisme bebek
