xlrustylx
09-02-2011, 05:18 AM
Hello,
I'm developing a theme on Wordpress and I have a custom function which pulls the URL, Title and Description of an image attachment of a post based on post ID and puts that information in an array which is returned when the function is ran:
function get_image_info($id) {
$images = get_children(array(
'post_parent' => $id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1));
foreach ($images as $image) {
$image_url = wp_get_attachment_url($image->ID);
$image_title = $image->post_title;
$image_desc = $image->post_excerpt;
$image_info = array(
'url' => $image_url,
'title' => $image_title,
'desc' => $image_desc);
return $image_info;
}
}
I call the code within a 'while' loop:
<?php while (have_posts()) : the_post(); ?>
<h3>
<?php the_title(); ?>
</h3>
<?php $image = get_image_info(get_the_ID()); ?>
<img style="height:150px;width:auto;float:left;" src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" />
<p>
<?php ob_start();
the_content();
$content = ob_get_clean();
echo truncate($content,400);?>
</p>
<a href="<?php the_permalink(); ?>">Read More</a>
<div class="clearfix"></div>
<?php endwhile; ?>
As you can see, within each iteration of the loop, the function 'get_image_info' is ran. It creates and returns an array containing the URL, Title and Description of the current post's attached image. I then use that array to fill the 'src' and 'title' properties of the image tag.
Through the first iteration of the 'while' loop, 'get_image_info' returns an array and it works properly, but no array is returned at all on all subsequent iterations of the loop. It's like something gets broken after the first iteration.
I can't figure out for the life of me what is going on, lol.
What's causing my code to fail? (besides my intermediate skill)
I'm developing a theme on Wordpress and I have a custom function which pulls the URL, Title and Description of an image attachment of a post based on post ID and puts that information in an array which is returned when the function is ran:
function get_image_info($id) {
$images = get_children(array(
'post_parent' => $id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1));
foreach ($images as $image) {
$image_url = wp_get_attachment_url($image->ID);
$image_title = $image->post_title;
$image_desc = $image->post_excerpt;
$image_info = array(
'url' => $image_url,
'title' => $image_title,
'desc' => $image_desc);
return $image_info;
}
}
I call the code within a 'while' loop:
<?php while (have_posts()) : the_post(); ?>
<h3>
<?php the_title(); ?>
</h3>
<?php $image = get_image_info(get_the_ID()); ?>
<img style="height:150px;width:auto;float:left;" src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" />
<p>
<?php ob_start();
the_content();
$content = ob_get_clean();
echo truncate($content,400);?>
</p>
<a href="<?php the_permalink(); ?>">Read More</a>
<div class="clearfix"></div>
<?php endwhile; ?>
As you can see, within each iteration of the loop, the function 'get_image_info' is ran. It creates and returns an array containing the URL, Title and Description of the current post's attached image. I then use that array to fill the 'src' and 'title' properties of the image tag.
Through the first iteration of the 'while' loop, 'get_image_info' returns an array and it works properly, but no array is returned at all on all subsequent iterations of the loop. It's like something gets broken after the first iteration.
I can't figure out for the life of me what is going on, lol.
What's causing my code to fail? (besides my intermediate skill)