How: Post list with image?
-
** I tried post in “WP-Advanced”, but only moderators can move things round **
Hey, folks. I’m trying do some lists (recent post, popular post, more commented…). Ok, that’s easy and working.
But I want return the first image from the latest post.
Example:Recent
Image Here- 3***
- 2
- 1
Popular Post
Image Here- First popular***
- 2nd popular
- 3rd popular
And so on. I wanted return the image from the latest post in the list (marked with ***)
The problem: code (below) almost worked. It returns image, but from the wrong post. From “oldest” instead the new one (1 and 3rd popular).
This feature is a widget, and yes. It has more code, but I guess only this part is important.
$ultimos = new WP_Query($query); $i=0; if ($ultimos->have_posts()) : while ($ultimos->have_posts()) : $ultimos->the_post(); $content = get_the_content(); //search image preg_match('/<img[^>]*>/i',$content,$matches); //process image if($matches){ preg_match('/src="[^"]*"/i',$matches[0],$src); preg_match('/width="(\d*)[^"]*"/i',$matches[0],$width); $img = "<img $src[0] align='center' title='".get_the_title()."' width='130px' height='113px' />"; } else $img = "<img align='center' width='130px' height='113px' src='".get_bloginfo('template_directory')."/images/semfoto.jpg' title='".get_the_title()."' />"; $titles[] = get_the_title(); $links[] = get_permalink();$i++; endwhile; ?> <div class="widget-img"> <h3><?php echo $instance['title'] ?></h3> <?php echo $img;?> <ul> <?php for($j=0;$j<count($titles);$j++) { ?> <li> <a href="<?php echo $links[$j] ?>" rel="bookmark"><?php echo $titles[$j]; ?></a> </li> <?php } ?> </ul> </div> <?php endif; }
Any highlighted? :/
- The topic ‘How: Post list with image?’ is closed to new replies.