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

[ASK] Replace Gambar di Wordpress

Discussion in 'Wordpress' started by fajarnet, Oct 7, 2014.

  1. fajarnet

    fajarnet N/A

    Joined:
    Jan 20, 2012
    Messages:
    979
    Likes Received:
    185
    Location:
    fb.com/fjrnug
    Gimana yah caranya replace image pada single post wordpress agar tampil setelah paragraf pertama.

    yang sebelumnya seperti ini :

    Code:
    <img>
    <p>Paragraf 1</p>
    <p>Paragraf 2</p>
    
    Jadi seperti ini :

    Code:
    <p>Paragraf 1</p>
    <img>
    <p>Paragraf 2</p>
    
    Kalau manual satu2 bakalan pegel juga soalnya ada lebih dari 8rb artikel.

    Matur suwun, terimakasih dan thank you atas jawabanya.
     
  2. wpdevep

    wpdevep Ads.id Pro

    Joined:
    Apr 6, 2012
    Messages:
    453
    Likes Received:
    92
    Location:
    Tebet, Jakarta Selatan
    harus manual, soalnya cara manggil artikel / paragraph aja kayak gini <?php the_content();?> :lol:
     
  3. danzig

    danzig Super Hero

    Joined:
    Jul 4, 2014
    Messages:
    836
    Likes Received:
    56
    Location:
    Youniverse
    8rb artikel?? Sedab bener tuh kl emang tpaksa harus manual om :D

    Ikutan nunggu solusinya ah. Kali ada mastah yg punya ilmunya nih
     
  4. golekdollar

    golekdollar Ads.id Fan

    Joined:
    Jun 19, 2013
    Messages:
    122
    Likes Received:
    3
    penjelasan hampir kek gini
     
  5. nasdin

    nasdin Super Hero

    Joined:
    May 28, 2013
    Messages:
    806
    Likes Received:
    182
    mungkin bisa dicoba, tambah di functions.php

    PHP:
    function getFirstImg$postID ) {
        
    $img '';
        if (
    get_post_thumbnail_id($postID)){ // jika ada post_thumbnail
            
    $img get_post_thumbnail_id($postID);
        } else { 
    //jika tidak ada post_thumbnail, pakai attachment
            
    $args = array(
            
    'numberposts' => 1,
            
    'order'=> 'DESC',
            
    'post_mime_type' => 'image',
            
    'post_parent' => $postID,
            
    'post_type' => 'attachment'
            
    );

            
    $attachment get_children($args,ARRAY_A);
            if (
    $attachment){
                
    $reKey array_values($attachment);
                
    $img $reKey[0]['ID'];
            }
        }
        
    $img wp_get_attachment_image($img,'large');
        return 
    $img;
    }
    function 
    replaceImage($content){
        global 
    $post;
        
    $content preg_replace('/<img[^>]+\>/i'''$content); // buang gambar
        
    $content explode('</p>',$content); // pecah paragraf
        
    $content[0] .= getFirstImg($post->ID); // insert gambar
        
    $content implode('</p>',$content); // re-join paragraf
        
    return $content;
    }
    add_filter('the_content','replaceImage');
    atau yg lebih simpel

    PHP:
    function replaceImage($content){
        
    $img preg_match('/<img[^>]+\>/i'$content,$pic);
        if (isset(
    $pic[0])){
            
    $content str_replace($pic[0], ''$content); // buang gambar
            
    $content explode('</p>',$content); // pecah paragraf
            
    $content[0] .= $pic[0]; // insert gambar
            
    $content implode('</p>',$content); // re-join paragraf
        
    }
        return 
    $content;
    }
    add_filter('the_content','replaceImage');
     
    Last edited: Oct 14, 2014
  6. m4zwahyu

    m4zwahyu Newbie

    Joined:
    Aug 30, 2012
    Messages:
    16
    Likes Received:
    0
    Udah ada yang cobain sesuai anjuran para mastah belum ya? klo dah ada yg coba, reviewnya donk biar ane juga bisa coba, mo langsung coba sendiri takut eror kan sayang. :D
     
  7. fajarnet

    fajarnet N/A

    Joined:
    Jan 20, 2012
    Messages:
    979
    Likes Received:
    185
    Location:
    fb.com/fjrnug
    saya coba dulu deh :D
     
  8. fajarnet

    fajarnet N/A

    Joined:
    Jan 20, 2012
    Messages:
    979
    Likes Received:
    185
    Location:
    fb.com/fjrnug
    Masih belum bisa om :(
     
  9. siprof

    siprof Reviewer

    Joined:
    Jun 4, 2011
    Messages:
    845
    Likes Received:
    28
    coba ente copy isi file single.php ente dimari, pake pastebin juga boleh kalo kepanjangan
     
  10. fajarnet

    fajarnet N/A

    Joined:
    Jan 20, 2012
    Messages:
    979
    Likes Received:
    185
    Location:
    fb.com/fjrnug
    Code:
    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @package _s
     * @since _s 1.0
     */
    
    get_header(); ?>
    
    <div id="primary" class="site-content">
                <div id="content" role="main">
    
                <?php while ( have_posts() ) : the_post(); ?>             
    
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
                            
                     
    
    <div class="cat-name-single">   <?php echo the_category(' - ','') ?> </div>
            <h1 class="entry-title"><?php the_title(); ?></h1>
    
            <div class="entry-meta">
                <?php _e( 'Posted on', '_s' ) ?><?php _s_posted_on(); ?> <span class="entry-meta-right"><?php comments_popup_link(__('No Comment'), __('1 Comment'), __('% Comments')); ?></span> | <span class="counter"><?php setPostViews(get_the_ID()); ?> <?php echo getPostViews(get_the_ID()); ?> Views</span>
    
            </div><!-- .entry-meta -->
        </header><!-- .entry-header -->
    
                    <?php if ( get_option_mmtheme('ad_6_on_off') == 'true' ) { ;?> 
                        <div class="ad6">
                            <?php echo stripslashes( get_option_mmtheme('ad_6') ) ?>          
                        </div> 
                    <?php } ?>
     
        <div class="entry-content">
            <?php the_content(); ?>
            <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', '_s' ), 'after' => '</div>' ) ); ?>
        </div><!-- .entry-content -->
    <div class="cb"> </div>
        <footer class="entry-meta">
            <?php the_tags(); ?>
         
    
            <span class="entry-meta-right">  <?php edit_post_link( __( 'Edit', '_s' ), '<span class="edit-link">', '</span>' ); ?></span>
        <div class="cb"></div>
        </footer><!-- .entry-meta -->
    </article><!-- #post-<?php the_ID(); ?> -->
    
    
    
    <?php if ( get_option_mmtheme('share_box') == 'true' ) { ;?> 
    <div class="social-media-single">
    <!-- AddThis Button BEGIN -->
    <div class="addthis_toolbox addthis_default_style ">
            <span>Share This</span>
            <a class="addthis_button_facebook_like" addthis:url="<?php the_permalink(); ?>" addthis:title="<?php the_title(); ?> - <?php the_permalink(); ?>"></a> 
            <a class="addthis_button_tweet" addthis:url="<?php the_permalink(); ?>" addthis:title="<?php the_title(); ?> - <?php the_permalink(); ?>"></a>
            <a class="addthis_button_google_plusone" g:plusone:size="medium" addthis:url="<?php the_permalink(); ?>" addthis:title="<?php the_title(); ?> - <?php the_permalink(); ?>"></a>
            <a class="addthis_button_pinterest_pinit" addthis:url="<?php the_permalink(); ?>" addthis:title="<?php the_title(); ?> - <?php the_permalink(); ?>"></a>
            <a class="addthis_counter addthis_pill_style" addthis:url="<?php the_permalink(); ?>" addthis:title="<?php the_title(); ?> - <?php the_permalink(); ?>"></a> 
            
            
    </div>
    <script type="text/javascript" src="http://s7.addthis.com/js/300/addthis_widget.js#pubid=xa-509e29782cc486d8"></script>
    <!-- AddThis Button END -->
    </div> 
    <?php } ?> 
    
    
                    <?php if ( get_option_mmtheme('ad_7_on_off') == 'true' ) { ;?> 
                        <div class="ad7">
                            <?php echo stripslashes( get_option_mmtheme('ad_7') ) ?>          
                        </div> 
                    <?php } ?>
    
     
    <?php if ( function_exists('about_this_author') ) { ;?> <div> <?php } ?>
    <?php if(function_exists('about_this_author')) { echo about_this_author(); } ?>        
    
    
    <?php if ( get_option_mmtheme('related_posts') == 'true' ) { ;?>  
    <div class="relatedposts-wrapper">
    <?php
    $categories = get_the_category($post->ID);
    if ($categories) {
        $category_ids = array();
        foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
        
        $args=array(
            'category__in' => $category_ids,
            'post__not_in' => array($post->ID),
            'showposts'=>  get_option_mmtheme('relatedpost_number'), // Number of related posts that will be shown.
            'caller_get_posts'=>1
        );$my_query = new wp_query($args);
    if( $my_query->have_posts() ) {
    echo '<div id="relatedposts"><h3 class="title-text"> Related Posts</h3><ul>';
    while ($my_query->have_posts()) {
    $my_query->the_post();
    ?>
    
    <?php ?>
    
    
    <?php
    if ( has_post_thumbnail() ) { ?>
    <li><div class="relatedthumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'related-thumb'); ?>
    <div class="relatedthumbtitle"><?php the_title(); ?></div></a></div></li>
    <?php } else { ?>
    <li><div class="relatedthumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><img src="<?php echo get_post_meta($post->ID, 'Image',true) ?>" width="196" height="110" alt="<?php the_title_attribute(); ?>" /><?php the_title(); ?></a></div></li>
    <?php }
    ?>
    <?php
    }
    echo '</ul> </div>';
    }
    }
    
    wp_reset_query();
    ?>
    </div>
    
    <?php } ?> 
    
            
    
                    <?php
                        // If comments are open or we have at least one comment, load up the comment template
                        if ( comments_open() || '0' != get_comments_number() )
                            comments_template( '', true );
                    ?>
                     
                     
                <?php endwhile; // end of the loop. ?>
    
                </div><!-- #content -->
            </div><!-- #primary .site-content -->
            
    <?php get_sidebar(); ?>
    <div class="cb"> </div>
    
    <?php get_footer(); ?>
    
    
    ini om.
     
  11. odevanty

    odevanty Ads.id Starter

    Joined:
    Aug 11, 2009
    Messages:
    67
    Likes Received:
    0
    Location:
    Bandung, Indonesia
    Gini gan, kadang ada theme WordPress yang menggunakan framework dari pengembangnya sendiri.
    Apalagi kalo theme mahal. rata-rata sudah pakai framework.

    Kalo saran mastah-mastah diatas belum bisa seperti cara mastah golekdollar dan nasdin

    coba share function.php

    dan yang perlu diketahui jika agan pakai framework, framework apa?
     

Share This Page