ถามการทำ thumbnail ของ post ใน WP หน่อยครับ

เริ่มโดย morris, 18 พฤษภาคม 2011, 14:38:45

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

morris

ถ้าเราไม่ใข้ plug-in ในการทำ thumbnail เราจะรู้ได้ยังไงว่า theme ที่เราใช้ทำได้ และถ้าทำไม่ได้เราจะต้องเพิ่มเติมไฟล์หรือcode อะไรเข้าไปครับ (ลองหากระทู้จากในบอร์ดแล้วครับ งง!)รบกวนผู้รู้ด้วยครับ

T l2 0 J A N

#1
ใช้ custom fields สร้าง field ชื่อ thumb ขึ้นมา แล้วใส่ url รูปลงไป

เวลาแสดงรูปออกมา ก็ใช้คำสั่ง

<a href="<?php $values get_post_custom_values("thumb"); echo $values[0]; ?>"><img src="<?php $values get_post_custom_values("thumb"); echo $values[0]; ?>" width="100" height="100" border="0"></a>
รับสกรีนเคส พิมพ์เคส ราคาถูกLikemecase.com

[direct=https://www.facebook.com/LikeMeCase]ทำเคส[/direct] [direct=https://www.facebook.com/LikeMeCase]สกรีนเคส[/direct] [direct=http://www.likemecase.com]สั่งทำเคส[/direct]

morris


Ping-O-Matic

#3
ปกติเวลาทำ Web ด้วย Wordpress จะไม่ใช้ฟังชั่น Thumbnail ที่ติดมากับตัว Wordpress เลยค๊ะ มันทำให้เปลือง พื้นที่ Host อะ แต่จะใช้วิธี ดึงรูปภาพรูปแรก ของโพส มาแทนค๊ะ ดึงมาแล้ว จะใช้โชว์ภาพขนาดเท่าไหร่ เราก็กำหนด ขนาดภาพเองตอนหลังได้ ก็แล้วแต่เรา

วิธีทำค๊ะ เปิด function.php แล้วใส่ Code ตามนี้ค๊ะ ปล ถ้าใส่ผิดตำแหน่ง มันจะ Error


// Get URL of first image in a post
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];

// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}


แล้วเวลาจะใช้ หมายถึง จะดึง URL ของรูป ไปแสดงที่จุดไหน ของ Wordpress ของเรา ก็ให้ใช้


<?php echo catch_that_image() ?>


ปล ที่มันดึงมา คือ ดึง URL ของรูปภาพนะค๊ะ ไม่ใช่ดึงภาพมา เช่น รูปภาพรูปแรกใน Post มี URL คือ hxxt://www.thaiseoboard.com/images/image-1.jpg มันก็จะดึงค่านี้มาให้ การทำไปใช้ก็ ใช้ Code Html แทรกรูป ตามปกติได้เลย ตามนี้ค๊ะ

<img src="<?php echo catch_that_image() ?>" alt="ใส่ Alt เองนะ"/>

ลองเล่นดูค๊ะ แล้วจะลืม Thumbnail ไปเลย  :wanwan016:

morris

สุดยอดมากครับ เดี๋ยวผมขอนั่งคลำดูก่อนนะครับ ไม่ได้ยังไงจะรบกวนอีกครับ ขอบคุณมากเลยครับ

gilbert

<?php if(get_post_meta($post->ID, 'Thumbnail' ,true) ): ?>
        //ถ้ามี Custom fields ชื่อ Thumnail
   <img src="<?php _e(get_post_meta($post->ID, 'Thumbnail',true) ) ?>" title="<?php the_permalink() ?>" />
<?php else : ?>
        //ไม่มีก็แสดงภาพ nopicture.jpg (หาพามาเองนะครับ)
   <img src="<?php bloginfo('template_url') ?>/images/nopicture.jpg" title="<?php the_permalink() ?>" />
<?php endif; ?>

morris


ekzakizm

#7
ไม่ทราบว่าคำสั่งนี้ใส่ตรงไหนครับ
               <img src="<?php echo catch_that_image() ?>" alt="ใส่ Alt เองนะ"/>


คือผมเอาไปใส่ใน index.php ระหว่าง

<h2><span>recently featured posts</span> <small>we've got <?php $count_posts wp_count_posts(); echo $count_posts->publish?> articles so far</small></h2>
<?php ?>
               
                <img src="<?php echo catch_that_image() ?>" alt="ใส่ Alt เองนะ"/>
               

<?php while (have_posts()) { the_post(); ?>

แต่ผลคือมันไปดึงภาพจากไหนมาไม่รู้กำหนดเองไม่ได้
และมันก็ใหญ่มากเลยอะคับ และมันก็ได้เฉพาะเรื่องแรก เรื่องต่อมามันไม่มี Thumbnail ให้อะคับ

Giffy

อ้างถึงจาก: ekzakizm ใน 22 พฤษภาคม 2011, 04:20:36
ไม่ทราบว่าคำสั่งนี้ใส่ตรงไหนครับ
               <img src="<?php echo catch_that_image() ?>" alt="ใส่ Alt เองนะ"/>


คือผมเอาไปใส่ใน index.php ระหว่าง

<h2><span>recently featured posts</span> <small>we've got <?php $count_posts wp_count_posts(); echo $count_posts->publish?> articles so far</small></h2>
<?php ?>
               
                <img src="<?php echo catch_that_image() ?>" alt="ใส่ Alt เองนะ"/>
               

<?php while (have_posts()) { the_post(); ?>

แต่ผลคือมันไปดึงภาพจากไหนมาไม่รู้กำหนดเองไม่ได้
และมันก็ใหญ่มากเลยอะคับ และมันก็ได้เฉพาะเรื่องแรก เรื่องต่อมามันไม่มี Thumbnail ให้อะคับ

ได้เอาโค๊ดนี้ไปใส่ใน function.php แล้วใช่มัียคะ?
อ้างถึง// Get URL of first image in a post
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]
  • ;

    // no image found display default image instead
    if(empty($first_img)){
    $first_img = "/images/default.jpg";
    }
    return $first_img;
    }
ใส่ต่อท้ายคำสั่งเกี่ยวกับ Thumbnail หนะค่ะ

ภาพที่ดึงได้มันจะดึงจากภาพในโพสนั้น

ส่วนการกำหนดขนาดภาพให้ใส่แบบนี้ค่ะ
อ้างถึง<img src="<?php echo catch_that_image() ?>" width="ความกว้างภาพ" height="ความยาวภาพ"/>

ekzakizm

ทำได้แล้วครับ ขอบคุณมากๆครับ
เป็นเพราะไม่ค่อยเข้าใจ ภาษา php ใน wp ใส่ผิดใส่ถูก จนเข้าใจครับ  :wanwan017:

Lioneye

ขอ บุ๊คมาร์คไว้ด้วยคนค่ะ หาคำตอบแบบนี้มานานจิงๆ มีหลายเว็บที่ต้องใช้วิธีดึงภาพแบบนี้แต่ทำไม่เป็นค่ะ
[direct=https://www.facebook.com/smdasset]บ้านมือสองนนทบุรี[/direct] บ้านมือสอง บ้านหลุดจำนอง
[direct=http://www.smdasset.com]บ้านมือสอง[/direct] บางกรวย ไทรน้อย ปากเกร็ด บางบัวทอง

morris

อ้างถึงจาก: Ping-O-Matic ใน 18 พฤษภาคม 2011, 22:07:09
ปกติเวลาทำ Web ด้วย Wordpress จะไม่ใช้ฟังชั่น Thumbnail ที่ติดมากับตัว Wordpress เลยค๊ะ มันทำให้เปลือง พื้นที่ Host อะ แต่จะใช้วิธี ดึงรูปภาพรูปแรก ของโพส มาแทนค๊ะ ดึงมาแล้ว จะใช้โชว์ภาพขนาดเท่าไหร่ เราก็กำหนด ขนาดภาพเองตอนหลังได้ ก็แล้วแต่เรา

วิธีทำค๊ะ เปิด function.php แล้วใส่ Code ตามนี้ค๊ะ ปล ถ้าใส่ผิดตำแหน่ง มันจะ Error


// Get URL of first image in a post
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];

// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}


แล้วเวลาจะใช้ หมายถึง จะดึง URL ของรูป ไปแสดงที่จุดไหน ของ Wordpress ของเรา ก็ให้ใช้


<?php echo catch_that_image() ?>


ปล ที่มันดึงมา คือ ดึง URL ของรูปภาพนะค๊ะ ไม่ใช่ดึงภาพมา เช่น รูปภาพรูปแรกใน Post มี URL คือ hxxt://www.thaiseoboard.com/images/image-1.jpg มันก็จะดึงค่านี้มาให้ การทำไปใช้ก็ ใช้ Code Html แทรกรูป ตามปกติได้เลย ตามนี้ค๊ะ

<img src="<?php echo catch_that_image() ?>" alt="ใส่ Alt เองนะ"/>

ลองเล่นดูค๊ะ แล้วจะลืม Thumbnail ไปเลย  :wanwan016:
ยังทำไม่ได้เลยครับ
1.โค็ดที่ใส่ใน function ใส่ตรงไหนครับ ลองใส่มั่วๆแล้ว เว็บ เอ๋อเหรอ เลย
2.โค็ดที่สองที่ใช้สำหรับการดึงรูปให้ไปใส่ใน index.php ใช่หรือเปล่าครับ
รบกวนผู้รู้อีกครั้งครับ

Giffy

อ้างถึงจาก: morris ใน 25 พฤษภาคม 2011, 20:11:29
อ้างถึงจาก: Ping-O-Matic ใน 18 พฤษภาคม 2011, 22:07:09
ปกติเวลาทำ Web ด้วย Wordpress จะไม่ใช้ฟังชั่น Thumbnail ที่ติดมากับตัว Wordpress เลยค๊ะ มันทำให้เปลือง พื้นที่ Host อะ แต่จะใช้วิธี ดึงรูปภาพรูปแรก ของโพส มาแทนค๊ะ ดึงมาแล้ว จะใช้โชว์ภาพขนาดเท่าไหร่ เราก็กำหนด ขนาดภาพเองตอนหลังได้ ก็แล้วแต่เรา

วิธีทำค๊ะ เปิด function.php แล้วใส่ Code ตามนี้ค๊ะ ปล ถ้าใส่ผิดตำแหน่ง มันจะ Error


// Get URL of first image in a post
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];

// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}


แล้วเวลาจะใช้ หมายถึง จะดึง URL ของรูป ไปแสดงที่จุดไหน ของ Wordpress ของเรา ก็ให้ใช้


<?php echo catch_that_image() ?>


ปล ที่มันดึงมา คือ ดึง URL ของรูปภาพนะค๊ะ ไม่ใช่ดึงภาพมา เช่น รูปภาพรูปแรกใน Post มี URL คือ hxxt://www.thaiseoboard.com/images/image-1.jpg มันก็จะดึงค่านี้มาให้ การทำไปใช้ก็ ใช้ Code Html แทรกรูป ตามปกติได้เลย ตามนี้ค๊ะ

<img src="<?php echo catch_that_image() ?>" alt="ใส่ Alt เองนะ"/>

ลองเล่นดูค๊ะ แล้วจะลืม Thumbnail ไปเลย  :wanwan016:
ยังทำไม่ได้เลยครับ
1.โค็ดที่ใส่ใน function ใส่ตรงไหนครับ ลองใส่มั่วๆแล้ว เว็บ เอ๋อเหรอ เลย
2.โค็ดที่สองที่ใช้สำหรับการดึงรูปให้ไปใส่ใน index.php ใช่หรือเปล่าครับ
รบกวนผู้รู้อีกครั้งครับ

1. ใส่ใกล้ๆกับ function ที่เกี่ยวกับ  Thumbnail ค่ะ
2. ใส่ตรงจุดที่ต้องการแสดงภาพค่ะ ไม่ว่าจะเป้นใน index , single page หรือที่อื่นๆที่เราจะดึงรูปจากบทความมาโชว์อะค่ะ

morris

ขอบคุณมากครับ เอ้า! จะลองดูอีกซักตั้ง

thailovesong

ขอบคุณมากครับ   :wanwan017:

ยังไม่มีเวปให้ลองเลย... :P

~อุอุ~


duodenum2

[direct=http://cheapbooksbuy.com/]Buy Cheap Books[/direct]
[direct=http://downloadbuysoftware.com/]Buy Download Software[/direct]
[direct=http://shoesdealscheap.com/]Cheap Shoes Deals[/direct]
[direct=http://giftsvalentinesales.com/]Valentine Gifts Sales[/direct]
[direct=http://womensbootsugg.com]Womens Boots UGG[/direct]

sbaydee

อ้างถึงจาก: ekzakizm ใน 22 พฤษภาคม 2011, 04:20:36
ไม่ทราบว่าคำสั่งนี้ใส่ตรงไหนครับ
               <img src="<?php echo catch_that_image() ?>" alt="ใส่ Alt เองนะ"/>


คือผมเอาไปใส่ใน index.php ระหว่าง

<h2><span>recently featured posts</span> <small>we've got <?php $count_posts wp_count_posts(); echo $count_posts->publish?> articles so far</small></h2>
<?php ?>
               
                <img src="<?php echo catch_that_image() ?>" alt="ใส่ Alt เองนะ"/>
               

<?php while (have_posts()) { the_post(); ?>

แต่ผลคือมันไปดึงภาพจากไหนมาไม่รู้กำหนดเองไม่ได้
และมันก็ใหญ่มากเลยอะคับ และมันก็ได้เฉพาะเรื่องแรก เรื่องต่อมามันไม่มี Thumbnail ให้อะคับ
แบบนี้ดีกว่า ผมทำประจำ

aCustiCz


ownermylife

[direct=http://www.รับพ่นสีฝุ่น.com]รับพ่นสีฝุ่น[/direct]