Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    33
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Custom function breaking within loop after one iteration

    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:

    Code:
    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:

    Code:
    <?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)

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,497
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Try it this way ... see the red bracket, I moved it so the return is out of the loop...


    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;
    }

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    33
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks. I played with it more last night and got it working.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •