WordPress.org

Ready to get started?Download WordPress

Forums

Featured story image, function not echoing correctly (3 posts)

  1. artfulalibi
    Member
    Posted 5 years ago #

    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!

  2. artfulalibi
    Member
    Posted 5 years ago #

    No one?

  3. alderete
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic