1. Halo Guest, pastikan Anda selalu menaati peraturan forum sebelum mengirimkan post atau thread baru.

Wordpress Auto Thumbnail Handling

Discussion in 'Wordpress' started by gembel-intelek, Oct 8, 2012.

  1. gembel-intelek

    gembel-intelek Lurker

    Joined:
    Mar 29, 2009
    Messages:
    4,341
    Likes Received:
    907
    Location:
    New Coral
    first check featured image ( post_thumbnail)
    next check attachment
    next check custom field (EXACT key)
    next check post content
    next check all custom field
    kalo ga ada, kasih default image


    function buat cek image. di sini ane hanya butuh url doang
    PHP:
    /**
     * Retrieve the URL of the current post image.
     *
     * @uses $post, get_post_custom, get_children, post_thumbnail, get_post_meta
     *
     * @return string, only return the url of image
     */
    function tp_get_post_thumb() {
        global 
    $post;
        
    $imgurl get_template_directory_uri() . '/images/default.jpg'// set default thumb, make sure this file exist
        
    $customField 'multiimg';                                      // this if you know the EXACT custom field key.
        
    $custom get_post_custom($post->ID);                            // Not sure with the key? grab all post custom instead.
        
    $thumbs get_children(array (                                  // we will check any attached images
            
    'post_parent' => $post->ID,
            
    'post_type' => 'attachment',
            
    'numberposts' => 1,
            
    'post_status' => null,
            
    'post_mime_type' => 'image',
        ));

        if (
    has_post_thumbnail()) { // check for featued image
            
    $id get_post_thumbnail_id();
            
    $imgurl wp_get_attachment_image_src($id'large');
            
    $imgurl $imgurl[0];
        } elseif (
    $thumbs) { // if no featured image, go with attached images
            
    foreach ($thumbs as $thumb) {
                
    $img wp_get_attachment_image_src($thumb->ID'large');
                
    $imgurl $img[0];
            }
        } elseif (
    get_post_meta($post->ID$customFieldtrue)) { // hmmm , nothing? let's try with a custom field key
            
    $imgurl get_post_meta($post->ID$customFieldtrue);
        }  elseif (
    preg_match('/src="(.*?)"/i'$post->post_content$match))
            { 
    // argggh.. still nothing! . scan entire post content
            
    $imgurl $match[1];
        }  elseif (
    get_post_custom($post->ID)) { // woam.. nothing there? last chance. lets pull out all post custom
            
    foreach ($custom as $v) {
                if (
    preg_match('!http://[a-z0-9_\-\.\/]+\.(?:jpe?g|png|gif)!Ui'$v[0], $matches)) {
                    
    $imgurl $matches[0];
                }
            }
        }
        return 
    $imgurl// note, only return URL, you should do your own home work!
    }
    ane ambil url doang karna mau di pake bersamaan timthumb

    PHP:
    /**
     * Show thumb for current post
     *
     * @uses timthumb.php
     *
     */
    function tp_show_thumb($height 60$width 60) {
        global 
    $post;
        
    $url get_permalink($post->ID);
        
    $imgurl tp_get_post_thumb();
        
    $timthumb get_template_directory_uri() . '/timthumb.php'// make sure this file exist
        
    $thumb '<a href="' $url '">';
        
    $thumb .= '<img alt="' $post->post_title '" width="' $width .'" height="' $height '" src="' $timthumb '?src=' $imgurl '&amp;h=' $height '&amp;w=' $width '&amp;zc=2&amp;q=80"/>';
        
    $thumb .= '</a>';
        echo 
    $thumb;
    }

    usage

    default size 60px height - 60px width
    PHP:
    <?php tp_show_thumb();?>
    kalo mau ukuran laen, ex : 250px height - 300px width
    PHP:
    <?php tp_show_thumb(250,300);?>
     
    Last edited: Oct 9, 2012
    mr mendem, masadi, biyanpasau and 6 others like this.
  2. yoedi

    yoedi Super Hero

    Joined:
    Nov 3, 2008
    Messages:
    2,766
    Likes Received:
    966
    Location:
    Milk City! Boyolali
    mastah2 theme and plugin developer wajib baca nih.. ckckckckck
     
  3. GrafitianZ

    GrafitianZ Super Hero

    Joined:
    Nov 6, 2010
    Messages:
    3,444
    Likes Received:
    202
    Location:
    JakCity
    Itu sama ga sama script ini?

    PHP:
    function get_post_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)){ 
          
    $img_dir get_bloginfo('template_directory');
        
    $first_img $img_dir '/images/thumb.gif';
      }
      return 
    $first_img;
    }
    ane biasanya make ini soalnya :D
     
  4. gembel-intelek

    gembel-intelek Lurker

    Joined:
    Mar 29, 2009
    Messages:
    4,341
    Likes Received:
    907
    Location:
    New Coral
    itu hanya 2 dari 6 kemungkinan , kalo buat personal use sih udah cukup
     
  5. khoid

    khoid Super Hero

    Joined:
    Aug 18, 2008
    Messages:
    3,380
    Likes Received:
    256
    Location:
    depok
    laen klo yg dikau itu, ngambil image dari wp_posts (content) klo yg puny gembel ngambil dari attachment. Cium Me If I Wrong :D
     
  6. gembel-intelek

    gembel-intelek Lurker

    Joined:
    Mar 29, 2009
    Messages:
    4,341
    Likes Received:
    907
    Location:
    New Coral

    idih maunya :muntah: sorry ye masih doyan cewe
     
  7. khoid

    khoid Super Hero

    Joined:
    Aug 18, 2008
    Messages:
    3,380
    Likes Received:
    256
    Location:
    depok
    beeeuhhhh klo di FB aje ngerayu2,., :monyong:
     
  8. ooND

    ooND Super Hero

    Joined:
    Jul 24, 2010
    Messages:
    5,707
    Likes Received:
    1,118
    Location:
    Wonosobo, Jawa Tengah, Indonesia, Indonesia
    konspirasi kah ini?
     
  9. nicefirework

    nicefirework Super Hero

    Joined:
    Aug 21, 2010
    Messages:
    1,304
    Likes Received:
    251
    super sekali mastah..
     
  10. airul

    airul Super Hero

    Joined:
    Nov 11, 2011
    Messages:
    2,256
    Likes Received:
    668
    Location:
    Magetan
    CTRL+D save dulu,,,, biasa otak atik theme,, pasti dibutuhin sewaktu waktu.. like penceted!
     
  11. paimin222

    paimin222 Ads.id Fan

    Joined:
    Apr 28, 2012
    Messages:
    108
    Likes Received:
    4
    keren mastah fungsi auto thumbnail nya :-bd
     
  12. arest

    arest Super Hero

    Joined:
    May 20, 2008
    Messages:
    2,288
    Likes Received:
    122
    Location:
    Krawu.net Hosting
    biar image nya clickable gmn yah mastah tq :D
     
  13. gembel-intelek

    gembel-intelek Lurker

    Joined:
    Mar 29, 2009
    Messages:
    4,341
    Likes Received:
    907
    Location:
    New Coral
    update di post 1
     
  14. masadi

    masadi Super Hero

    Joined:
    Dec 12, 2010
    Messages:
    2,334
    Likes Received:
    144
    Location:
    mas-adi.com
    wah mastah maho udah dateng... jadi keluar topik nih.... :lol:
     
  15. GrafitianZ

    GrafitianZ Super Hero

    Joined:
    Nov 6, 2010
    Messages:
    3,444
    Likes Received:
    202
    Location:
    JakCity
    biasanya ane maen auto2 n gan ,makanya pake itu :D
    Tapi buat manual pastinya mantab bnget kode yg agan share :senyum:
    ane cowok tulen gan :silau:
    masa jeruk makan jeruk :lol:
     
  16. Igoey1122

    Igoey1122 Super Hero

    Joined:
    Sep 12, 2010
    Messages:
    1,245
    Likes Received:
    177
    Location:
    Di Hati Mu
    ini dia, yang ane cari thanks mastah gembel-intelek :kembang:
     
  17. cbpartner

    cbpartner Super Hero

    Joined:
    Nov 9, 2010
    Messages:
    2,294
    Likes Received:
    48
    Location:
    Warnet dekat rumah
    mantaps mastahhh... ane baru nyicipin dan langsung work tanpa (error)!!!
     

Share This Page