WordPressPHP

WordPress Iteration in Your Own PHP Application

Oleh takien• 14 Komentar Arsip

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. :D

The following PHP code contains some basic functions to make your own WordPress Loop.

Code

Wordpress-style Loop

$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; 

The advantageof 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 :D

Information Thanks to Matt Huggins post at Stackoverflow and xrvel post at kaskus

Tulisan ini sudah berumur: 16 tahun 6 bulan 12 hari. Informasi di dalamnya mungkin sudah tidak relevan, tapi nilai historisnya sangat berharga, terutama bagi saya sebagai penulisnya.

(Dipulihkan dari takien.com)

Komentar (14)

Arsip Statis
Kacang Hijau
Sial, kok titlenya “Pedagang bubur kacang ijo” :ngakak Bro btw nggak install emoticon kaskus?
takien
Pedagang buburnya antar benua bro, mau kan Emo kaskus, hmm.. gk cocok ama theme nya dong hehhe
Fauzie
nice post dude… just what i needed…
dholie
ko ane coba ga bisa ya gan? ga ada tampilan na, cuma keluar klo di liat view page na,., :’( query na ane isi biasa, “select * from post” apa bukan gitu ya? boleh minta script yg dah jagi ga? sama struktur db na? thanks sebelum na
takien
FYI, kode ini bukan untuk wordpress… tapi untuk php buatan sendiri yang mau nampilin isi database dengan cara seperti yang dilakukan wordpress… untuk query nya terserah, tapi untuk menjalankan contoh diatas, at least tabel mempunyai kolom Title dan Content karena akan di panggil di sini: [php] function the_title() { global $post; return $post->Title; //field Title } function the_content() { global $post; return $post->Content; //field Content }[/php]
dholie
ga bisa jg gan,., :’( ane coba pake database field na : ID, Title, Content query na aku pake “select * from table” tapi tetep ga bisa, ga keluar apa2 cuma layar putih duank :’(
takien
Seharusnya bisa loh.. atau coba query langsung dulu, nampil gak jangan2.. isi database nya emang kosong heheh..
dholie
database na ada isi na lah gan :’( yg keluar cuma tag h2 duank (klo diliat source page na) oh iya ane mo nanya, knp ga pake mysql_fetch_array ya? ato script diatas itu ada pengganti mysql_fetch_array? klo iya, yg bagian mana ya,., sorry banyak nnya,., biar ga sesat di dunia maya
takien
hmmm… bentar,ku cek lagi
iroel
Tahu fungsi parse_str kan gan? Klo mo bikin gitu kayak gimana yah? Jadi outputnya bisa ditaruh di argumen ato variabel global. Jawabnya di kaskus trid PHP ya gan. Jarang mampir ke sini soalnya. Ini aja mampir pertama kli
takien
hehe, aku gk pernah pake parse_str bro, tapi pake nya wp_parse_str, yang ada di WP.. coba deh.. keren n lebih lengkap..
Prast Rahastu
gan
Domain register
Nice presentation Thank you for this information, for a website now a days very much important all the user
sisme bebek
Great review