?
Solved

get the first image

Posted on 2011-09-26
2
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 total points
ID: 36713527
Try

echo get_the_title( $post->post_parent );

and

echo get_permalink( $post->post_parent );
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

I want to start by talking about the use of plug-ins for WordPress. I started a web-site for a company I was working for a few years ago; I had extremely basic knowledge of HTML. I am a Graphic Designer by trade so I invited the opportunity as a cha…
What's this? As a volunteer Page Editor for Experts-Exchange.com, I have noticed that many authors also have blogs, and pull articles from their blogs to post at Experts-Exchange. I appreciate each author sharing their blog content with our site …
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…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

764 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