How can I properly loop through an array gotten from my wordpress database and display it to my site

I am trying to display some posts information from my database on my live site. I have done the query correctly because I can view the items on the live site. But I need to take three items to display. The post title, the post content which houses the images, and the post id which links to the posts. I seem to have written it correctly but it does not do anything.

This is the code

<?php 
    global $wpdb;               
    $results = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE `post_type`='post' AND `post_status` = 'publish' LIMIT    20" );

  foreach ($results) {
   
  echo "<div class="card">
            <div class="img">
             <img src="{post_content}" alt="">
            </div>
            <div class="info">
               <h5>{post_title}</h5>
            </div>
          </div>";
  }
   //echo "<pre>";print_r($results);echo"</pre>";
                    
?>

I am quite new to wordpress and PHP in general.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try this

global $wpdb;               
    $results = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE `post_type`='post' AND `post_status` = 'publish' LIMIT    20" );

  foreach ($results as $result) {
 
  echo '<div class="card">
            <div class="img">
             <img src="'.get_the_post_thumbnail_url( $result -> ID, 'thumbnail' ).'" alt="">
            </div>
            <div class="info">
               <h5>'.$result->post_title.'</h5>
            </div>
          </div>';
  }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x