Add thumbnails into foreach loop list

<ul class="section-list">

I’m trying to get the thumbnail to appear for each list item. However, I only get this outputted

<?php the_post_thumbnail(); ?> and it it commented out.

		<?php
			$args = array( 'numberposts' => '15', 'post_status' => 'publish' );
			$recent_posts = wp_get_recent_posts( $args );
			
			foreach( $recent_posts as $recent ){
				echo  '<li>
<?php the_post_thumbnail(); ?>
<a href="' . get_permalink( $recent["ID"] ) . '"     	  title="'.esc_attr( $recent["post_title"] ).'" >' . $recent["post_title"].'</a> </li> ';
			}
		
?>
</ul><!-- .section-list -->

I think you need to escape your quotations. Also, the_post_thumbnail(); is ALREADY inside of <?php tags so doing it again is probably what is breaking it. Try this?

foreach( $recent_posts as $recent ){
				echo  "<li>".the_post_thumbnail()."<a href=".get_permalink($recent[\"ID\"])."title=".esc_attr( $recent[\"post_title\"]).">".$recent[\"post_title\"]."</a> </li>";
			}

Thanks, but that just broke the page. It seems like this would be easier, but I am new to php

I’m pretty sure the_post_thumbnail needs to be inside the WordPress loop. Try this (untested):

<?php foreach( $recent_posts as $recent ): ?>
    <li>
        <?php echo get_the_post_thumbnail( $recent['ID'] ); ?>
            <a href="<?php echo get_permalink( $recent['ID'] ); ?>" title=" <?php echo esc_attr( $recent['post_title']); ?>">
        <?php echo $recent['post_title']; ?>
        </a>
    </li>
<?php endforeach; ?>

it will probably be more helpful if I show all of the code

<ul class="section-list">
			
<?php
								$args = array(
									'orderby' => 'name',
									'order' => 'ASC'
								);

								$categories = get_categories( $args );
								foreach( $categories as $category ) {
									echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s", "thebreakingnews" ), $category->name ) . '" ' . '>' . $category->name.'</a> ('.$category->count.') </li>';
								}
							?>
							</ul><!-- .section-list -->
						</div><!-- .custom-archive-item -->

						<div class="custom-archive-item">
							<h3><?php _e( 'Contributors', 'thebreakingnews' ); ?></h3>
							<ul class="section-list">
							<?php
								$args = array(
									'show_fullname' => true,
									'optioncount' => true,
									'orderby' => 'post_count',
									'order' => 'DESC'
								);

								wp_list_authors($args);
							?>
							</ul><!-- .section-list -->

I’m going to try this

I recently made this example which may be relative. The images are favicon icons which are scraped from a directory.

http://www.johns-jokes.com/downloads/sp-d/cssbonding/

This can be help http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

I’ve looked at this, but still can’t get it to work.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.