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

[help] modifikasi script get_first_image() di WordPress

Discussion in 'Wordpress' started by zuhair, Aug 6, 2010.

  1. zuhair

    zuhair Ads.id Fan

    Joined:
    Jul 28, 2010
    Messages:
    156
    Likes Received:
    2
    alo gan.... saya dapet script ini dari blog orang luar untuk misahin & nempatin ulang posisi image pertama yang ada di sebuah postingan. trus saya bikin modifikasinya kayak gini dg tambahan script thimthumb:

    file functions.php:
    PHP:
    function get_first_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 get_bloginfo('template_url').'/images/nopic.gif';

      }
      return 
    $first_img;
    }
    file index.php:
    PHP:
    <!-- slider -->
    <div class="slider">
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php echo get_first_image() ?>" alt="<?php the_title(); ?>" height="300" width="600" /></a>
    </div>

    <!-- content -->
    <div class="content">
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo get_first_image() ?>&h=80&w=150" alt="<?php the_title(); ?>" /></a>
        <?php content(); ?>
    </div>
    yang jadi masalah saya, kalau untuk gambar di bagian slider, saya sudah pas dengan settingan seperti itu. tapi untuk gambar yang di bagian content, saya masih kurang pas ketika ada suatu postingan yang ga punya gambar, karena ngikut settingan get_first_image() maka dia akan tetap menampilkan gambar nopic.gif. sedangkan saya maunya untuk bagian content dia ga usah nampilin gambar nopic seperti di bagian slider.

    kira-kira coding seperti apa yang perlu saya tambahkan di bagian contentnya ya? Soalnya kalau misalkan saya bikin 2 coding seperti get_first_image1() dan get_first_image2() rasanya kok lucu gitu ya... ehehehe :D


    mohon bantuannya ya ... makasih banyak!!

    --- Update ---

    waduh...blm ada yang jawab :nangis:

    cari bantuan dulu ahh...
     
  2. khoid

    khoid Super Hero

    Joined:
    Aug 18, 2008
    Messages:
    3,380
    Likes Received:
    256
    Location:
    depok
    yg di content di tambahin pemisalan aja
    HTML:
    <div class="content">
    <?php if(substr(get_first_image(),-9)!="nopic.gif"){?>
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo get_first_image() ?>&h=80&w=150" alt="<?php the_title(); ?>" /></a>
    <?php } ?>
        <?php content(); ?>
    </div>
    coba sendiri yak,., belom ane coba soal na,.,., :swt2:

    ***permasalahan na, tiap image yg nama belakang na nopic.gif ga bakal muncul :D
     
    zuhair likes this.
  3. zuhair

    zuhair Ads.id Fan

    Joined:
    Jul 28, 2010
    Messages:
    156
    Likes Received:
    2
    masih ga bisa gan... :nangis:

    apa jangan2 karena script thimthumbnya ya?? :hmm:
     
  4. khoid

    khoid Super Hero

    Joined:
    Aug 18, 2008
    Messages:
    3,380
    Likes Received:
    256
    Location:
    depok
    ga ngaruh ko ma thimbthumb mesti na,.,.,
    yg di function ente sama kya yg ente tulis di atas kan??? :hmm:
     
  5. zuhair

    zuhair Ads.id Fan

    Joined:
    Jul 28, 2010
    Messages:
    156
    Likes Received:
    2
    iya gan... sama persis..siss...sis...

    atau masalah sama loop?
     
  6. khoid

    khoid Super Hero

    Joined:
    Aug 18, 2008
    Messages:
    3,380
    Likes Received:
    256
    Location:
    depok
    dah ane coba tapi ga pake thimthumb bisa ko ye om?? ente pake theme ape???
     
  7. waskitho

    waskitho Ads.id Pro

    Joined:
    Jun 11, 2009
    Messages:
    299
    Likes Received:
    24
    Location:
    jogja
    lha maunya nampilin apa? samasekali tak ada gambar?
    yang enak sih fungsinya aja yg dimodif.
     
    zuhair likes this.
  8. zuhair

    zuhair Ads.id Fan

    Joined:
    Jul 28, 2010
    Messages:
    156
    Likes Received:
    2
    dah coba pake & ga pake thimthumb tetep ga bisa... gimana ni bos?? :nangis:
    lagi nyoba bikin themes sendiri sih...
    semuanya dah bekerja dg baik, tinggal masalahnya itu aja antara munculin & ga munculin gambar :hmm:

    jadi gini gan... satu fungsi itu ane pake di 2 tempat dalam file yang sama yaitu index.php. tapi kondisinya mau dibedakan.
    fungsi untuk bagian slider itu mau ane jadiin kalau ada gambar di postingan ybs, maka gambar itu dimunculkan. tapi kalau ga ada gambar di postingan ybs, maka yang dimunculkan gambar yang udah ane set di functions.php
    sementara fungsi untuk bagian content itu kalau ada gambar di postingan ybs, maka gambar itu dimunculkan. tapi kalau ga ada gambar, maka ga ada gambar apapun yang muncul di postingan itu. alias langsung ke postingan text.

    jadi gimana tuh gan kalo mau ngubah fungsinya?
     
  9. dCartes

    dCartes Super Hero

    Joined:
    Jan 21, 2010
    Messages:
    1,424
    Likes Received:
    131
    Location:
    Citizen of the world
    Kalo ane simple aja bro :senyum:
    Tambahin parameter di functionnya:

    get_first_image($param)

    Kalau $param isinya: slider, di kasih nopic.gif.
    Kalau $param isinya: content, di kasih pic lainnya.
    Bisa digunakan utk byk tempat tuh function, dgn bermacam2 $param.
    Jadi functionnya di ubah dikit jadi gini:

    file functions.php:
    PHP:
    function get_first_image($param) {
      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
        
    if ($param == 'slider')
          
    $first_img get_bloginfo('template_url').'/images/nopic.gif';
        elseif (
    $param == 'content') { 
          
    // Misalnya utk content
          
    $first_img get_bloginfo('template_url').'/images/content.gif';
        }
        else {
          
    // Utk bagian laen selain di atas, kosongin
          
    $first_img ''
        }

      }
      return 
    $first_img;
    }
    Lalu pemanggilannya tinggal isi $param-nya dengan string: slider, content, dll.

    file index.php:
    PHP:
    <!-- slider -->
    <div class="slider">
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php echo get_first_image('slider'?>" alt="<?php the_title(); ?>" height="300" width="600" /></a>
    </div>

    <!-- content -->
    <div class="content">
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo get_first_image('content'?>&h=80&w=150" alt="<?php the_title(); ?>" /></a>
        <?php content(); ?>
    </div>
     
    zuhair likes this.
  10. zuhair

    zuhair Ads.id Fan

    Joined:
    Jul 28, 2010
    Messages:
    156
    Likes Received:
    2
    nah, coding bro dCartes dah mendekati nih... tapi masalahnya dia masih ninggalin jejak meskipun gambarnya ga ditampilin. yaitu, jejak gambar yang berborder (karena settingan ane gambarna dikasih border) dan juga jejak alt-nya.

    trus pas ane cek dari source code juga masih keliatan jejaknya gan...
    Code:
    <a href="[URL="http://www.adsense-id.com/forums/view-source:http://localhost/wordpress/index.php/6-lorem-ipsum-1.html"]http://localhost/wordpress/index.php/lorem-ipsum.html[/URL]" title="Lorem ipsum"><img src="[URL="http://www.adsense-id.com/forums/view-source:http://localhost/wordpress/wp-content/themes/saiful-haq/scripts/timthumb.php?src=&h=80&w=137"]http://localhost/wordpress/wp-content/themes/mytheme/scripts/timthumb.php?src=&amp;h=80&amp;w=150[/URL]" alt="Lorem ipsum"></a>
    gimane nih gan biar ga ninggalin jejak?




    *kasih like dulu ah buat semua yang ikut komen disini..
     
  11. dCartes

    dCartes Super Hero

    Joined:
    Jan 21, 2010
    Messages:
    1,424
    Likes Received:
    131
    Location:
    Citizen of the world
    Itu tetep ninggalin jejak, krn ada <a> dan <img> yg 'kosong' isinya.
    Kalau gitu di pecah jadi 2 aja functionnya.

    file functions.php:
    PHP:
    function cek_first_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];
      return 
    $first_img;
    }

    function 
    get_first_image($param) {
      
    $first_img cek_first_image();

      if(empty(
    $first_img)){ // Defines a default image
        
    if ($param == 'slider')
          
    $first_img get_bloginfo('template_url').'/images/nopic.gif';
        elseif (
    $param == 'content') { 
          
    // Misalnya utk content
          
    $first_img get_bloginfo('template_url').'/images/content.gif';
        }
        else {
          
    // Utk bagian laen selain di atas, kosongin
          
    $first_img ''
        }

      }
      return 
    $first_img;
    }
    Terus panggil cek_first_image() utk cek ada img nya atau gak.
    Kalau ada img-nya, baru ditampilkan.

    file index.php:
    PHP:
    <!-- slider -->
    <div class="slider">
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php echo get_first_image('slider'?>" alt="<?php the_title(); ?>" height="300" width="600" /></a>
    </div>

    <!-- content -->
    <div class="content">
        <?php if (cek_first_image()): ?>
            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo get_first_image('content'?>&h=80&w=150" alt="<?php the_title(); ?>" /></a>
        <?php endif; ?>
        <?php content(); ?>
    </div>
     
  12. zuhair

    zuhair Ads.id Fan

    Joined:
    Jul 28, 2010
    Messages:
    156
    Likes Received:
    2
    muantebb gan!! it works!!!:kembang:
    makasih banyak ya!!! :salaman:
     
  13. dCartes

    dCartes Super Hero

    Joined:
    Jan 21, 2010
    Messages:
    1,424
    Likes Received:
    131
    Location:
    Citizen of the world
    Sama2 :)

    The message you have entered is too short. Please lengthen your message to at least 30 characters.
     
  14. waskitho

    waskitho Ads.id Pro

    Joined:
    Jun 11, 2009
    Messages:
    299
    Likes Received:
    24
    Location:
    jogja
    sebenernya gak dipecah juga bisa
     
  15. zuhair

    zuhair Ads.id Fan

    Joined:
    Jul 28, 2010
    Messages:
    156
    Likes Received:
    2
  16. waskitho

    waskitho Ads.id Pro

    Joined:
    Jun 11, 2009
    Messages:
    299
    Likes Received:
    24
    Location:
    jogja
    gini. fingsinya sedikit modif. mirip punya bro dCartes

    PHP:
    function get_first_image($blank=false) {
      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)&&!$blank){ //Defines a default image
        
    $first_img get_bloginfo('template_url').'/images/nopic.gif';

      }
      return 
    $first_img;
    }
    trus manggilnya gini:

    PHP:
    <!-- slider -->
    <div class="slider">
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php echo get_first_image() ?>" alt="<?php the_title(); ?>" height="300" width="600" /></a>
    </div>

    <!-- content -->
    <div class="content">
        <?php $firstimg=get_first_image(1); if (!empty($firstimg)): ?>
            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo $firstimg ?>&h=80&w=150" alt="<?php the_title(); ?>" /></a>
        <?php endif; ?>
        <?php content(); ?>
    </div>
    semoga berhasil... belom saya coba soalnya ... hehehehe
     
  17. zuhair

    zuhair Ads.id Fan

    Joined:
    Jul 28, 2010
    Messages:
    156
    Likes Received:
    2
    bisa gan!

    wah...bener2 makasih banyak nih...!!


    *btw, kok tombol Like ane ilang ya gan?? jadi ga bisa ngasih Like lagi nih.. mo ngasih reputasi juga dibatesin ya sehari sekali? kemaren dah abis jatahnya klo gitu buat agan dCartes :nangis:
     

Share This Page