• I know I can call the function following:

    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];

    if(empty($first_img)){ //Defines a default image
    $first_img = “/images/default.jpg”;
    }
    return $first_img;
    }

    But I try to using this function in single.php or posting page. It was display only image at own ID.

    However, I need to call an image from next and previous post of this ID.

    Can I do it?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter bonzaiya

    (@bonzaiya)

    Thanks!!

    However, I don’t want to using Featured Image. I want to using a first image that found in next and previous post to display in currently post.

    Yes, use regex there to get first one.

    Thread Starter bonzaiya

    (@bonzaiya)

    Thanks!

    Can you help me to write function of it?

    I really doubt about function. I try to write and edit some value but it not return a value or get errors not displayed next and previous image by post.

    This is my single.php code:

    It seems to display information of own page only.

    <?php get_header(); ?><?php if ( have_posts() ) : ?>
    
    <?php /* Start the Loop */ ?><?php while ( have_posts() ) : the_post(); ?>
    
    <?php get_template_part( 'content', get_post_format() ); ?>
    <?php endwhile; ?>
    
    <?php /* Start the Loop */ ?><?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <div class="productwarp-feature">
    
    <div class="productimage">
    
    <div align="center"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><img src="<?php echo catch_that_image() ?>" style="width: 80px; max-height: auto !important; height: 80px; max-width: auto !important; margin-left: -30px;"></a></div>
    
    </div>
    
    <div class="productinner"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></div>
    
    <p class="blurb" style="text-align: center;"><a href="<?php echo catch_that_url() ?>" rel="nofollow" title="Go to Store" class="purelycss_fusion_button color_green" style="text-decoration: none; color: white;" target="_blank">Shop Now</a></p>
    
    </div>
    <?php endwhile; ?>
    </div>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to: Get the first image from the next and previous post in single.php?’ is closed to new replies.