0

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.

1
  • Question should be moved to stackoverflow, this is a code related issue. Sep 20, 2020 at 11:54

1 Answer 1

0

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>';
  }
2
  • This works but I only get to see the first post. Sep 20, 2020 at 9:45
  • I know what the problem is. The CSS styles arent getting applied. I would work on this. Thank you. Sep 20, 2020 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.