Solved

get the first image

Posted on 2011-09-26
2
217 Views
Last Modified: 2012-05-12

Hi Guys,

i am trying to get the first image from all the posts in my blog.
i need to show them all in one page where i have a custom jquery slideshow.
i am trying to understand how is it possible to loop on all the posts and their first attached photo.

i found 2 scripts while searching and i am embedding them here, none of the works!
can someone direct me?

1st script:
=================================
function echo_first_image ($postID)
{					
	$args = array(
	'numberposts' => 1,
	'order'=> 'ASC',
	'post_mime_type' => 'image',
	'post_parent' => $postID,
	'post_status' => null,
	'post_type' => 'attachment'
	);
	
	$attachments = get_children( $args );
	
	//print_r($attachments);
	
	if ($attachments) {
		foreach($attachments as $attachment) {
			$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' )  ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );
				
			echo '<img src="'.wp_get_attachment_thumb_url( $attachment->ID ).'" class="current">';
			
		}
	}
}

Open in new window


2nd code
=============================

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

// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}

Open in new window

0
Comment
Question by:Refael
2 Comments
 

Author Comment

by:Refael
ID: 36672590

Okay i found this code event better and short, but i need to grab the post title and to link it to the post (not the image)


 
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null ); 
$attachments = get_posts( $args );
if ($attachments) {	
	
	foreach ( $attachments as $post ) {
		setup_postdata($post);
		?>
        <div><?php echo the_title(); ?>
        <?php
		the_attachment_link($post->ID, false);
		the_excerpt();
	}
}

Open in new window


thanks!
0
 
LVL 31

Accepted Solution

by:
gwkg earned 500 total points
ID: 36713527
Try

echo get_the_title( $post->post_parent );

and

echo get_permalink( $post->post_parent );
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Do you think that WordPress is just for blogs?  Think again!  WordPress is really a fantastic all around platform that you can use to develop websites on.  Integrated into its basic functionality is the ability to create pages using your choice of a…
WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question