Created
October 13, 2012 06:11
-
-
Save leoken/3883460 to your computer and use it in GitHub Desktop.
Create A Loop Of Images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
<?php | |
if (have_posts()) : | |
while (have_posts()) : the_post(); ?> | |
<a href="<?php the_permalink();?>" title="<?php the_title(); ?>" class="img-loop"> | |
<img src="http://media.smashingmagazine.com/wp-content/uploads/images/wordpress-loop-hacks/<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /> | |
</a> | |
endwhile; | |
endif; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://wp.smashingmagazine.com/2009/05/13/10-custom-fields-hacks-for-wordpress/