• Hi Guys I want to know if I could specify a size for the image displayed in this code. Please help me!!!!

    <?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    $args = array(
    	'posts_per_page' => 1,
    	'posts_status' => array( 'publish', 'future' ),
    	'paged' => $paged,
    	'cat' => 15,
    );
    // the query
    $the_query = new WP_Query( $args );
    ?>
    
    	<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    	<div class=" display-posts-listing .listing-item">
    <?php echo catch_that_image() ?>
    <?php the_content(); ?>
    </div>
    
    	<?php endwhile; ?>
    
    <?php
        wp_pagenavi( array( 'query' => $the_query ) );
    ?>
    	<?php else: endif; ?>

    Really thanks the help from keesiemeijer who helped me to get the code to this level!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • If this is the code you are using:

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

    Then the line <?php echo catch_that_image() ?> is only going to echo the path to the image. That means you can wrap it in an img tag and specify a class, id or inline styling.

    i.e.
    <img class="firstimage" src="<?php echo catch_that_image() ?>" />
    or
    <img style="height: 200px; width: 300px;" src="<?php echo catch_that_image() ?>" />

    Thread Starter goloops

    (@goloops)

    thank you so much greendemiurge, i will going to give it a try later when i reach home ??

    Thread Starter goloops

    (@goloops)

    Thanks greendemiurge, the code works great. Any idea how to change the code to make the images link to the respective post?

    <img style="height: 200px; width: 300px;" src="<?php echo catch_that_image() ?>" />

    Thanks a lot!!

    Thread Starter goloops

    (@goloops)

    I managed to find this code

    <a href="<?php the_permalink() ?>"> <img style="height: 200px; width: 300px;" src="<?php echo catch_that_image() ?>" />

    And it works ok...:)

    Awesome. That’s how you do it.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to specify a size for the image displayed?’ is closed to new replies.