admin管理员组文章数量:1323035
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.
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.
Share Improve this question asked Sep 20, 2020 at 9:17 Chinomso JohnsonChinomso Johnson 32 bronze badges 1- Question should be moved to stackoverflow, this is a code related issue. – amarinediary Commented Sep 20, 2020 at 11:54
1 Answer
Reset to default 0Try 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>';
}
版权声明:本文标题:php - How can I properly loop through an array gotten from my wordpress database and display it to my site 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742109089a2421162.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论