บทความนี้ผมเขียนขึ้นมาสำหรับคนที่สนใจจะพัฒนา WordPress หรือว่าปรับแต่ง Theme ของตัวเองนะครับผม ซึ่งการทำเว็บไซต์ด้วย WordPress เนี่ยมันดีมากๆตรงการจัดการเนื้อหาและที่สำคัญ SEO เยี่ยมมากเลยหล่ะครับ ติด Google เร็วเวอร์ๆ มาดูกันนะครับว่า กรณีต้องการให้มีรูป Thumbnails แสดงให้เห็นในหน้าจัดการเนื้อหามีวิธีการอย่างไร (ดูภาพประกอบนะครับ ว่าผลลัพธ์จะออกมาแนวไหน)
อยากลองดูไหมละครับ ถ้าว้อนท์แล้ว ก็ก๊อบปี้โค้ดด้านล่างนี้แหล่ะคับไปแปะในไฟล์ที่อยู่ในโฟลเดอร์ ชื่อ Theme/functions.php ได้เลยครับ
/****** Add Thumbnails in Manage Posts/Pages List ******/ if ( !function_exists('AddThumbColumn') && function_exists('add_theme_support') ) { // for post and page add_theme_support('post-thumbnails', array( 'post', 'page' ) ); function AddThumbColumn($cols) { $cols['thumbnail'] = __('Thumbnail'); return $cols; } function AddThumbValue($column_name, $post_id) { $width = (int) 60; $height = (int) 60; if ( 'thumbnail' == $column_name ) { // thumbnail of WP 2.9 $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true ); // image from gallery $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') ); if ($thumbnail_id) $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true ); elseif ($attachments) { foreach ( $attachments as $attachment_id => $attachment ) { $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true ); } } if ( isset($thumb) && $thumb ) { echo $thumb; } else { echo __('None'); } } } // for posts add_filter( 'manage_posts_columns', 'AddThumbColumn' ); add_action( 'manage_posts_custom_column', 'AddThumbValue', 10, 2 ); // for pages add_filter( 'manage_pages_columns', 'AddThumbColumn' ); add_action( 'manage_pages_custom_column', 'AddThumbValue', 10, 2 ); }
ถ้าหากว่าคุณต้องการปรับเปลี่ยนขนาดของ thumbnails คุณก็เพียงแค่เปลี่ยนค่าของตัวแปร $width และ $height ที่อยู่ในด้านบนแค่นั้นเองครับ จากนั้นก็กดเซฟไฟล์ functions.php ไปโลด แล้วลองไปหน้า Admin ไปที่เมนู Post ก็จะเห็นว่ามี Thumbnails ประกอบบทความขึ้นมาแล้ว วิธีการทำไม่ยากเลยใช่ไหมละครับ ขอให้สนุกกับการใช้งาน WordPress นะคร้าบ