WordPress.org

Support

Support » How-To and Troubleshooting » Featured story image, function not echoing correctly

Featured story image, function not echoing correctly

  • Hello everyone,

    I have pieced together two small functions. One that grabs an image from an attached post, and second which creates a list of featured posts using a category.

    I thought it would be no problem to simply reference my image function with in my feature function, and list a set of featured stories with their image, with the image and text as a hyperlink.

    However, the image seems to pop out of the echo element.

    Here are my functions:

    //post image function
    function postimage($size=thumbnail) {
    	if ( $images = get_children(array(
    		'post_parent' => get_the_ID(),
    		'post_type' => 'attachment',
    		'numberposts' => 1,
    		'post_mime_type' => 'image',)))
    	{
    		foreach( $images as $image ) {
    			$attachmentimage=wp_get_attachment_image( $image->ID, $size );
    
    			echo $attachmentimage;
    		}
    	} else {
    		echo '<!-- No image -->';
    	}
    }
    
    //Featured Story
    function featuredStory() {
    $featuredPosts = new WP_Query();
    $featuredPosts->query('cat=9&showposts=5');
    while ($featuredPosts->have_posts()) : $featuredPosts->the_post();
    
    echo '<li><a href="' .get_permalink(). '" rel="bookmark">' . postimage(thumbnail) . the_title('', '', 0) . '</a></li>';
    
    endwhile;
    }
    ?>

    And here is the resulting HTML:

    <img src="http://domain/wp-content/uploads/2008/10/81023w1_gomez_s_b_gr_03-100x100.jpg" width="100" height="100" class="attachment-thumbnail" alt="" />
    <li><a href="http://domain/?p=13" rel="bookmark">A gallery test</a></li>

    As you can see the image function gets placed before any of the echoed HTML elements, so it looses it’s hyperlink and styling.

    Any help would be great!

    Thanks everyone!

Viewing 2 replies - 1 through 2 (of 2 total)
  • No one?

    You’ve basically written echo(“X” . echo(“Y”)), which is always going to print “YX” to the screen, because echo() emits its arguments immediately, which is before you want it to do so. You will _never_ get the results you expect when you embed an echo statement inside another echo statement.

    In your postimage() function, try _returning_ the value you want for the image reference, instead of echo()ing it in the function itself.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Featured story image, function not echoing correctly’ is closed to new replies.
Skip to toolbar