• Home
  • Tools
  • Sitemap
  • Blogroll
  • RSS My Aggregator
  • Cek Pagerank
  • About

  • Membuat Related Post Wordpress Dengan PHP

    December 25th, 2009 Under Internet, Wordpress | 146 Comments

    Untuk menampilkan related post pada wordpress kebanyakan blogger, menggunakan plugin seperti wordpress related plugin, yarpp, related post by category dkk seperjuangannyalah. Namun setelah saya selidiki (ala) plugin-plugin ini lumayan banyak memakan resource pada server hostingan khususnya buat yang menggunakan paket shared hosting dan mempunya postingan yang lumayan banyak.

    Untuk menghindari akibat-akibat yang tidak diinginkan dan untuk mengoptimalisasikan engine wordpress , ada baiknya sebagai alternatif menggunakan kode php untuk meggantikan plugin-plugin tersebut. Secara mudahnya buka singel.php pada theme yang anda gunakan dan tambahkan kode php berikut. (Sebelumnya Backup dulu file single.php anda).

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    <?php
    $this_post = $post;
    $category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
    $posts = get_posts('numberposts=6&offset=0&orderby=post_date&order=DESC&category='.$category);
    $count = 0;
    foreach ( $posts as $post )
    {
    if ( $post->ID == $this_post->ID || $count == 5)  <!-- banyak  related post yang ditampilkan -->
    {
    unset($posts[$count]);
    }
    else
    {
    $count ++;
    }
    }
    ?>
    <?php if ( $posts ) : ?>
    <div>
    <strong>Related Posts</strong> <!-- Judul, bisa diganti -->
    <ul>
    <?php foreach ( $posts as $post ) : ?>
    <li><a href="<?php the_permalink() ?>" title="<?php echo trim(str_replace("n"," ",preg_replace('#<[^>]*?>#si','',get_the_excerpt()))) ?>"><?php if ( get_the_title() ){ the_title(); }else{ echo "Untitled"; } ?></a></li>
    <?php endforeach // $posts as $post ?>
    </ul>
    </div>
    <?php endif // $posts ?>
    <?php
    $post = $this_post;
    unset($this_post);
    ?>
    1
    2
    3
    4
    5
    6
    7
    <?php the_content(__('')); ?>

    -->pastekan kira-kira disini,

    sesuaikan saja dengan theme yang anda gunakan

    <divclass="meta-footer">

    Kode php tersebut akan menampilkan 5 postingan yang terkait, sesuai dengan kategori postingan anda. Jika anda menggunakan kode php untuk melihat request query pada database wordpress, lihat saja perbandingan antara menggunakan plugin dan kode php.



    You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    146 Responses to 'Membuat Related Post Wordpress Dengan PHP'

    1. alamendah December 25, 2009 at 6:59 am
      alamendah
    2. alamendah December 25, 2009 at 7:00 am
      alamendah
    3. alamendah December 25, 2009 at 7:01 am
      alamendah
    4. aldy December 25, 2009 at 9:15 am
      aldy
    5. aldy December 25, 2009 at 9:17 am
      aldy
    6. kyra.curapix December 25, 2009 at 9:49 am
      kyra.curapix
    7. yangputri December 25, 2009 at 12:49 pm
      yangputri
    8. Desri December 26, 2009 at 11:42 am
      Desri
    9. Desri December 26, 2009 at 11:43 am
      Desri
    10. kips December 26, 2009 at 12:09 pm
      kips

    Leave a Reply

    :hehe: :hore: :jedug: :love: :matabelo: :mikir: :hiks: :ngacir: :ngakak: :nyerah: :plis: :puyeng: :sikut: :sip:

    Need Another Emo..??