Support » Plugins » Hacks » Display post thumbnail inside of php output

  • I am very basic when it comes to php and I wanted to modify the php code below to display the post image before the permalink.

    The original code below of the plugin:

    function posts_list () {
    	$output= '<ul>';
    	$posts = get_posts('numberposts=-1');
    	foreach($posts as $post){
    		$permalink = get_permalink( $post->ID );
    		$output.= '<li>' . '<a href="' . $permalink . '">' . $post->post_title . '</a></li>';
    	return $output;

    Then i tried the following:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    lol your probably laughing at my attempts but how do I separate them for it to work? I tried adding . and quotes nothing worked. I get a fatal error plugind disabled.

Viewing 5 replies - 1 through 5 (of 5 total)
  • add the_post_thumbnail( 'thumbnail' ); after the { and before $permalink = …

    Thanks gerbik, it didnt show errors but it created numerous images of the same post image. Not the featured images of the permalinks. perhaps its more complex than I thought.

    You need to use get_the_post_thumbnail with your function to get a return value instead of a printed one, here’s a tweaked version of your function.

    function posts_list () {
    	$output = '';
    	$posts = get_posts( 'numberposts=-1' );
    	foreach( $posts as $post )
    		$output .= '<li>' . get_the_post_thumbnail( $post->ID ) . '<a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></li>';
    	$output = '<ul>' . $output . '</ul>';
    	return $output;

    wow thats awesome Mark! It worked great! This looks like fun, the possibilities with coding. 😀

    Appreciate it!

    You’re welcome. 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display post thumbnail inside of php output’ is closed to new replies.