WordPress: Membuat Related Post Tanpa Plugin

Written by Fikri Rasyid

Saya ingin membuat bagian related post namun saya tidak ingin menggunakan plugin apapun untuk melakukannya. mengapa? Tentu saja untuk membuat performa situs lebih cepat, dong :D

Biasanya, saya akan menggunakan tutorial WP Recipes ini untuk membuat related post: http://www.wprecipes.com/how-to-show-related-posts-without-a-plugin. Jadi sederhananya yang dilakukan adalah “mengambil” tag pertama yang dimiliki oleh post yang sedang dibuka, lalu munculkan post2 dengan tag yang sama dengan mengecualikan post yang sekarang tengah dibuka . It works well, kecuali kenyataan bahwa satu post bisa saja memiliki lebih dari satu tag.

Lalu saya googling dan ketemulah dengan halaman support WordPress ini: http://wordpress.org/support/topic/show-posts-with-same-tags-as-current-single-post idenya simple, ‘mengambil’ semua ID post tagĀ  yang dimiliki oleh post yang sedang diakses dan menyatukannya dengan fungsi PHP array_push. tapi entah mengapa kurang bekerja dengan kurang baik. Setelah saya lihat2 sepertinya disebabkan oleh nama variabel yang tidak dibedakan.

Jadi saya tweak sedikit dan inilah hasilnya. Bekerja dengan baik, tinggal di paste di dalam loop utama di file single.php:

<?php
$tags = wp_get_post_tags($post->ID);

if ($tags) {
$alltags = array();
foreach( $tags as $tag ) {
array_push( $alltags, $tag->term_id );
}

$args=array(
'tag__in' => $alltags,
'post__not_in' => array($post->ID),
'showposts'=>4,
'caller_get_posts'=>1,
'orderby' => 'rand'
);

$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>

<p><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></p>

<?php
endwhile;
}
}
?>

About The Author

Fikri Rasyid - I speak HTML + CSS + jQuery, breath in world wide WordPress-land, co-founded WPNest and currently pursuing my bachelor degree majoring English Education at Indonesia University of Education. Google my name for more information about me.

Connect & Subscribe

Keep in touch and get my latest content trough:

4 Responses for This Thought

  1. ArdianZzZ

    03 October 2010

    Haha, tepuk tangan buat Textpattern karena hanya perlu sebuah tag untuk menampilkan telated post..
    <txp:related_articles />
    :)

    Reply
    • fikrirasyid

      03 October 2010

      wah, textpattern bisa sesimple itu? hihi, jadi tertarik :p

      Reply
  2. pRista Fantasia

    02 January 2011

    kalo untuk wordpress gratisan bisa ndak ya mas ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>