WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Include Feature image (3 posts)

  1. cjc1867
    Member
    Posted 1 year ago #

    Hi
    I got this code from a tutorial that adds a number of child pages to a parent page but it didn't include a thumbnail and I need the thumbnail. I have added the extra code for the thumbnail.

    $subpages = new WP_query($args);
    	// create output
    	if ($subpages->have_posts()) :
    		$output = '<ul>';
    		while ($subpages->have_posts()) : $subpages->the_post();
                $output .= '<li>'.the_post_thumbnail('thumbnail',array('style'=>'float:left;margin-right:10px;'));
    			$output .= '<strong><a href="'.get_permalink().'">'.get_the_title().'</a></strong>
    						<p>'.get_the_excerpt().'<br />
    						<a href="'.get_permalink().'">Continue Reading →</a></p></li>';
    		endwhile;
    		$output .= '</ul>';
    	else :
    		$output = '<p>No subpages found.</p>';
    	endif;

    When I view the page the thumbnail images all come back outside the loop, why?

    Link: http://www.westlancscarerscentre.org.uk/support-services/

    Colin

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    the code uses string concatenation to prepare the output, while you added a function that immediately prints the output;

    use get_the_post_thumbnail() http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

    only the one line shown after the edits:

    $output .= '<li>'.get_the_post_thumbnail($post->ID,'thumbnail',array('style'=>'float:left;margin-right:10px;'));

    afaik, 'style' is not one of the supported attributes and might cause problems (?)

  3. cjc1867
    Member
    Posted 1 year ago #

    Thanks for your help, I came across another thread on here and they used get_the_post_thumbnail() so I changed it to that and it works.

    Still learning so I'm very grateful for you helping me out.

    Thanks

    Colin

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.