Support » Fixing WordPress » Include Feature image

  • Resolved cjc1867


    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>';
    		$output .= '</ul>';
    	else :
    		$output = '<p>No subpages found.</p>';

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



Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael


    Forum Moderator

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

    use 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 (?)

    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.



Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Include Feature image’ is closed to new replies.