WordPress Iteration in Your Own PHP Application
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
Information
Incoming search terms:
- wordpress post_count
- related:blackbooksingles com/ blackbooksingles com
- keluar dari looping php
- keluar dari while sql
- langsung keluar dari while php
- source code php keluar
- source code while loop pada sql
-
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
