WP Tips Display Related Posts With Thumbnails!

0

Comments

Related Posts คือปลั๊กอินที่แสดงบทความที่เกี่ยวข้องกับคำค้นที่ใกล้เคียงกัน และสุ่มมาแสดงที่หน้าบทความโดยการสุ่ม แต่ Related Posts ตัวนี้พ่วง Thumbnails! หรือจะแสดงรูป Thumbnails หน้าบทความนั้นด้วย ระบบนี้ผมก็ไม่เคยลองนะครับและไม่แน่ใจว่าจะใช้ได้หรือเปล่า แต่เห็นบล็อกนอกหลายๆ เว็บนะที่ใช้ระบบนี้ มันไม่ใช่ปลั๊กอินนะครับ แต่เป็น Hacks โมดูล มาเริ่ม Hack กันเลยครับ เปิดไฟล์ single.php ที่อยู่ในธีมของคุณไฟล์ single.php นี้แต่ละธีมอาจจะวางโค้ดไม่เหมือนกัน แต่ในที่นี้ผมจะขอวางโค้ดไว้ฝต้ the_content() วางด้านล่าง

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$original_post = $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
  echo '<h2>Related Posts</h2>';
  $first_tag = $tags[0]->term_id;
  $args=array(
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
    'showposts'=>4,
    'caller_get_posts'=>1
   );
  $my_query = new WP_Query($args);
  if( $my_query->have_posts() ) {
    echo "<ul>";
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <li><img src="<?php bloginfo('template_directory'); ?>/timthumb/timthumb.php?src=<?php echo get_post_meta($post->ID, "post-img", true); ?>&h=40&w=40&zc=1" alt="" /><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;
    echo "</ul>";
  }
}
$post = $original_post;
wp_reset_query();
?>



ตัวอย่าง >>

< ,,>

Leave a Reply