นำรูปมาโชว์หน้าแรกของบทความใน wordpress

wordpress

วิธีนี้เป็นวิธีการดึงรูปในบทความของเรานำออกมาแสดงในหน้าแรกของ wordpress ยังที่เห็นกันในเว็บบล็อกต่างๆทั่วไป ซึ่งสามารถช่วยให้สื่อความหมายของบทความที่เราเขียนได้มากทีเดียว

1. ให้นำโค็ดนี้ไปเพิ่มใน wp-content/themes/[ชื่อ theme ของเรา]/funtion.php

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}

2. จากนั้นไปที่ไฟล์ index.php ของ theme หา

<div class="entry">
 <?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

แก้เป็น

<div class="entry">
 <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" class="dis-pic" />
 <?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

จกานั้นเราก็จัด css ได้ตามใจชอบ
wordpress

Via : http://wordpress.org/support/topic/246893

บทความแนะนำ
    blog comments powered by Disqus